activerecord

How to invalidate has_many relationship in Rails

In my Rails app, I have a class with a has_many relationship. For the sake of efficiency, I want to do some direct SQL to update many rows in the database at once, and then I want to mark the has_many relationship as no longer valid. If later code accesses the has_many relationship, I want it to reload the data. But I obviously want t...

notifying the user about successful update in codeigniter

Hi Guys, i'm using these two lines to update my table using codeigninter active records $this->db->where('reference_number', $reference); $this->db->update('patient', $data); what i want to de is to check weather it successfully updates the table and according to that i want to give a notification to the user, how can i check the su...

Help in ActiveRecord find with include on conditions

I have a Coach Model which: has_many :qualifications I want to find all coaches whose some attribute_id is nil and they have some qualifications. Something which is like. def requirement legal_coaches = [] coaches = find_all_by_attribute_id(nil) coaches.each do |coach| legal_coaches << coach if coach.qualification...

how to localize the active record error messages

Hi everyone, I try to figure out how I can localize the error item name in my rails application which appear when a user sign's up with uncorrect datas... I figured out how to override the messages but not the names of the messages like ("password", "login", "email", ...) de: activerecord: errors: models: user: ...

Ruby equivalent of ActiveRecord Group by query

Hi, I want to perform an operation on a array returned from an ActiveRecord query. This is the functionality I would have done on the ActiveRecord directly. Modification.find(:all, :group=>'ref_id,start_date', :order=>'updated_at desc') The above is working fine as expected. But the problem is I cannot perform it directly for some re...

Bi-directional polymorphic join model in Rails?

I'm working on a multi-site CMS that has a notion of cross-publication among sites. Several types of content (Articles, Events, Bios, etc) can be associated with many Sites and Sites can have many pieces of content. The many-to-many association between content pieces and sites must also support a couple common attributes for each conte...

Rails child object query leads to N+1 problem

Hi, while writing some application for personal use. I find out the child query is not as great as it look. For instance, I have 2 object Category has_many Files File belongs_to Category File.category will access its parent category. But this lead to the famous N+1 problem. For example, I want my homepage to list out 20 newest file...

Codeigniter Activerecord update method refuses to insert NULL value

I'm using Codeigniters Active record library to carry out an update on a column on my DB. Here's the SQL for the table CREATE TABLE `schedules` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `reservation_id` INT(11) NULL DEFAULT NULL, `title` VARCHAR(255) NOT NULL, `description` VARCHAR(512) NULL DEFAULT NULL, `start_date` DATE NOT NULL, `st...

CodeIgniter ActiveRecord class_name

I usually use a php class to create value objects for my database objects. Something like: class UserVO { public $id; public $email; } and I then return this as follows: $data = mysql_query($sql); while ($row = mysql_fetch_object($data, "UserVO")) { $result[] = $row; } return $result; I’m now trying to get to grips w...

Elegant PostgreSQL Group by for Ruby on Rails / ActiveRecord

Trying to retrieve an array of ActiveRecord Objects grouped by date with PostgreSQL. More specifically I'm trying to translate the following MySQL querry: @posts = Post.all(:group => "date(date)", :conditions => ["location_id = ? and published = ?", @location.id, true], :order => "created_at DESC") I am aware that PostgreSQL ...

System DSN not working when running Rails as Windows Service

When I run the mongrel server in a command window everything works fine, database connections are made and the app is running properly. If I set the Windows Service to run my app by logged in with my user credentials it works fine as well. However, when I set the Service to run as Local System I cannot get the application to start an...

Best way to handle 404 in Rails3 controllers with a DataMapper get

It's very simple, I want to handle a normal [show] request with a call to DataMapper like I did in Merb. With ActiveRecord I could have done this: class PostsController def show @post = Post.get(params[:id]) @comments = @post.comments unless @post.nil? end end and it handles the 404 by catching the resource's exceptions. ...

Why ActiveRecord::Base.connected? is false, after calling establish_connection

I develop Sinatra application and use there ActiveRecord for working with database, but I encountered one problem. I wrote a test for a model and it breaks with SQLite3::CantOpenException: unable to open database file Connection to database is established in test_helper.rb with the following code: Dir.chdir('..') do ActiveRecord::Ba...

What is the difference between these two statements, and why would you choose them?

I'm a beginner at rails. And I've come to understand two different ways to return the same result. What is the difference between these two? And what situation would require you to choose one from the other? Example 1: Object.find(:all).select {|c| c.name == "Foobar" }.size Example 2: Object.count(:conditions => ['name = ?', 'Fooba...

ActiveRecord condition syntax question.

Is there a better way of writing this? Is it possible to do cleanly in one line? conditions = ["category = ?", params[:category]] if params[:category] @events = CalendarEvent.all( :conditions => conditions ) ...

Rails optimization Question

In Rails while using activeRecord why are join queries considered bad. For example Here i'm trying to find the number of companies that belong to a certain category. class Company ActiveRecord::Base has_one :company_profile end Finding the number of Company for a particular category_id number_of_companies = Company.fin...

Issues with has_many :through, cache, touch and counter_cache

I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers. The problem is, that join table entries are deleted, not destroyed. If relation is gone, I have ...

Rails ActiveRecord - inheriting from a base class with no table

What i'm looking to do is have a base class for some of my models that has some default activerecord behavior: class Option < ActiveRecord::Base has_many :products def some_method #stuff end #etc..etc.. end class ColorOption < Option #stuff... end class FabricOption < Option #stuff... end However, I want ColorOpti...

Retrieving unique associated models from an array of another model

What is the recommended approach for finding multiple, unique associated models for a subset of another model? As an example, for a subset of users, determine unique artist models they have favorited. One approach is to grab the users from the database, then iterate them all quering for favorites and building a unique array, but this se...

activerecord create_table like existing table

With Rails/ActiveRecord 2.3.8 I'd like to do: AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' ) But AR insists on adding "()" to the generated SQL even though the field list is blank since the table DDL is being cloned, thus resulting in e.g.: ActiveRecord::StatementInvalid: M...