RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

How do I use "includes" in a Rails 3 AREL statement?

I am trying to see the SQL behind and AREL statement:

Brand.where(:subdomain => "coke").includes(:products).to_sql

brand has_many products, and product belongs_to brand.

However, the above statement yields only:

"SELECT `brands`.* FROM `brands` WHERE `brands`.`subdomain` = 'coke'"

Why don't I see the information for the products table in the SQL?

Upvotes: 2

Views: 6326

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124429

When you use an includes statement, Rails will generate a separate query per table (so if you include 2 other tables, there will be 3 queries total).

You can use a joins statement instead and it will lump it all into one query, however you may experience a performance hit. Also, if any of your where(...) conditions query against the included table, it will be lumped into one query.

See this other similar question for more information on Rails' behavior.

Upvotes: 7

Related Questions