Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails .includes() returning 'undefined method'

I have a Company object with an associated Location.

Company has_many :locations Location belongs_to :company

When I call:

@company = Company.find(5).includes(:locations)

I get the error:

NoMethodError: undefined method `includes' for #<Company:0x000000066ad398>

I'm following the rubyonrails.org instructions to a T... as far as I can tell.

like image 886
Shelby S Avatar asked Feb 27 '26 16:02

Shelby S


1 Answers

find() triggers a database query, and returns an instance of the model. You can't call includes on it, as includes is supposed to have an ActiveRecord::Relation as receiver.

You need to invert the order: find must stay at the end:

@company = Company.includes(:locations).find(5)
like image 79
Simone Carletti Avatar answered Mar 02 '26 05:03

Simone Carletti