Rails3: ActiveRecord includes(:relation) not working

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

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, Gerrit

Calling .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.)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs