[ rails ] ActiveRecord default_scope, scope

default_scope

default_scope{ order(created_at :desc)}

ref : http://apidock.com/rails/ActiveRecord/Base/default_scope/class

scope

scope :created_before, ->(time) { where("created_at < ?", time) }
scope :created_before, lambda { |time|
where("created_at &lt; ?", time)
}

scope can be chainable

scope :published, -> { where(published: true) }
scope :published_and_commented, -> { published.where("comments_count > 0") }

ref : http://guides.rubyonrails.org/active_record_querying.html,

http://stackoverflow.com/questions/8476627/what-do-you-call-the-operator-in-ruby

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s