ruby on rails - ActiveRecord condition override and default_scope -


there anyway around override default_scope or previous scope condition? (beside restructure model not use default_scope or use unscoped)

and knowns if there reason behind why works way does? feels not expected or intuitive result.

sample:

class product < activerecord::base   default_scope -> { visible }   scope :visible, -> { where(visible: true) }   scope :hidden, -> { where(visible: false) } end 

when this:

product.hidden 

the generated sql tries match 2 values:

where "products"."visible" = 't' , "products"."visible" = 'f' 

the same goes without default_scope:

class product < activerecord::base   scope :visible, -> { where(visible: true) }   scope :hidden, -> { where(visible: false) } end 

when this:

product.visible.where(visible: false) 

or when this:

product.visible.hidden 

the generated sql tries match 2 values:

where "products"."visible" = 't' , "products"."visible" = 'f' 

i made gist complete test case: https://gist.github.com/mmontossi/dcf71457e98a169c28a5

this issue when first asked thinking bug: https://github.com/rails/rails/issues/20907#issuecomment-122131096

there 1 thing need know default_scope , this: don't use it.

default_scope drug. @ first feels great, , seems easier, time goes on realize using bad idea. late, , must forever deal consequences. ok not entirely true: stop using default_scope , rewrite controllers explicitly call actual scopes, should have done in first place, because way obvious data being retrieved , less confusing other developers (including future forget using default_scope). default_scope not bad getting hooked on drugs. feels way sometimes.

just "no" default_scope.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -