method

includes

includes(*args)
public

No documentation available.

# File activerecord/lib/active_record/relation/query_methods.rb, line 15
    def includes(*args)
      args.reject! {|a| a.blank? }

      return self if args.empty?

      relation = clone
      relation.includes_values = (relation.includes_values + args).flatten.uniq
      relation
    end

1Note

Links and basic explanation

dancinglightning ยท Jan 2, 20123 thanks

This function, available on any Base derived class, allows eager loading. So when iterating over a result, all the data can be pre-cached, reducing the number of queries from 1+x to 2.

Several ways to use this include:

Post.includes(:author).where(..)

Post.includes([:author, :comments]).where(..)

Post.includes( :comments => :replies ).where(..)

The result is a Relation, so where and other function may be called.

.includes(:comments).to_a is the same as , .find(:all, :includes => :comments)

Some good documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Apperently one can get similar results by using joins, but i'm not expert on joins, so please ammend and read: http://guides.rubyonrails.org/active_record_querying.html