views:

456

answers:

1

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!

+1  A: 

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:

default_scope :include => :product

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.

EmFi
Thanks, at the end this really solves my problem. I knew about eager loading. My problem with that was that I needed to convert the result set to json and Rails' to_json method adds to the json output just the attributes that were selected in the query. However, I found a workaround - to_json method is very configurable and allows me to add the attributes from the eager loaded association very easily. Thanks again!
Milan Novota