instance method
includes
Ruby on Rails 4.0.13
Since v3.0.20Signature
includes(*args)
Specify relationships to be included in the result set. For example:
users = User.includes(:address) users.each do |user| user.address.city end
allows you to access the address attribute of the User model without firing an additional query. This will often result in a performance improvement over a simple join.
conditions
If you want to add conditions to your included models you’ll have to explicitly reference them. For example:
User.includes(:posts).where('posts.name = ?', 'example')
Will throw an error, but this will work:
User.includes(:posts).where('posts.name = ?', 'example').references(:posts)
Parameters
-
argsrest
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 116
def includes(*args)
check_if_method_has_arguments!("includes", args)
spawn.includes!(*args)
end
Defined in activerecord/lib/active_record/relation/query_methods.rb line 116
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::QueryMethods