Hey everyone, i can''t build AR-Queries with relationships between tables. Here''s a short sample: class Customer < ActiveRecord::Base has_many :projects end class Project < ActiveRecord::Base belongs_to :customer end When I execute the following Code in my Controller: @customers Customer.all.includes(:projects) a NoMethodError (undefined method `includes'' for #<Array:0xb679e030>) is thrown. Why did i receive an Array and not an Object of the class ActiveRecord? Thanks in advance for your help! Greets, Gerrit -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dylan Markow
2010-Apr-26 03:37 UTC
Re: Rails3: ActiveRecord includes(:relation) not working
Wanderwelten wrote:> Hey everyone, > > i can''t build AR-Queries with relationships between tables. Here''s a > short sample: > > class Customer < ActiveRecord::Base > has_many :projects > end > class Project < ActiveRecord::Base > belongs_to :customer > end > When I execute the following Code in my Controller: @customers > Customer.all.includes(:projects) > a NoMethodError (undefined method `includes'' for #<Array:0xb679e030>) > is thrown. > > Why did i receive an Array and not an Object of the class > ActiveRecord? > > Thanks in advance for your help! > > Greets, GerritCalling .all performs the query and returns the array. You need to put the includes() statement before the .all (and you actually don''t need the all anyway, it''ll know to load all of them unless you supply a limit, conditions, etc.) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.