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?
When you use an
includesstatement, 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
joinsstatement instead and it will lump it all into one query, however you may experience a performance hit. Also, if any of yourwhere(...)conditions query against the included table, it will be lumped into one query.See this other similar question for more information on Rails’ behavior.