Default_scope with: join and: select

I tried to define default_scope as follows:

default_scope :joins => :product, :select => "catalog_products.*, products.*"

What I get from Rails, but here's what:

 SELECT catalog_products.* FROM `catalog_products` INNER JOIN `products` ON `products`.id = `catalog_products`.product_id 

When I define it as 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 a mistake or is this the correct behavior?

I am using Rails 2.3.4.

Thanks!

+3
source share
1 answer

This is a fixed behavior. Regardless of how you define the scope, you can get class objects defining the scope back. Although you select model columns, Rails will not do anything with them. However, you can support boot associations. This is what it looks like you're trying to do, with a product on every find.

, :

default_scope :include => :product

, select , , , rails . SELECT, - find, , .

+5

Source: https://habr.com/ru/post/1721420/


All Articles