mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
0a12a5f816
Don't use this: scope :red, where(color: 'red') default_scope where(color: 'red') Use this: scope :red, -> { where(color: 'red') } default_scope { where(color: 'red') } The former has numerous issues. It is a common newbie gotcha to do the following: scope :recent, where(published_at: Time.now - 2.weeks) Or a more subtle variant: scope :recent, -> { where(published_at: Time.now - 2.weeks) } scope :recent_red, recent.where(color: 'red') Eager scopes are also very complex to implement within Active Record, and there are still bugs. For example, the following does not do what you expect: scope :remove_conditions, except(:where) where(...).remove_conditions # => still has conditions
3 lines
93 B
Ruby
3 lines
93 B
Ruby
class WithoutTable < ActiveRecord::Base
|
|
default_scope -> { where(:published => true) }
|
|
end
|