I tried to define a default_scope in the following way:
default_scope :joins => :product, :select => "catalog_products.*, products.*"
What I'm getting from Rails though is this:
SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id
When I define it as a named_scope, everything is fine:
named_scope :extended, :joins => :product, :select => "catalog_products.*, products.*"
SELECT catalog_products.*, products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id
Is this supposed to be a bug or is it a correct behavior?
I'm using Rails 2.3.4.
Thanks!
This is the indended behaviour. Regardless of how you define the scope you can only get objects of the class defining the scope back. Despite the fact that you are selecting off model columns, Rails won't do anything with them. However you can eager load associations. Which is what it looks like you are trying to do, with product on every find.
In fact it's simpler than you expect:
The reason your select statement is part of the query in the named scope but not the default scope is that every rails based query overrides the select options of the queries further up the method chain. The only SELECT options that make any difference to what's returned by the find statements are those that select a subset of the model's columns.