scope 的作用就是將時常使用或是複雜的ORM語法組合成懶人包,這樣下次要用的時候只要把懶人包拿出來就可以了,舉例說明:
class Topic < ActiveRecord::Base
scope :recent, -> { order("created_at DESC") }
end
上面這段code我們定義了recent
這個scope,以後我們只要下recent
這個指令就等於下order("created_at DESC")
是一樣的。如此一來就可以讓程式碼更為簡潔。
class Post < ActiveRecord::Base
scope :published, -> { where(published: true) }
end
class Post < ActiveRecord::Base
scope :created_before, ->(time) { where("created_at < ?", time) }
end
class Event < ActiveRecord::Base
scope :published, -> { where(published: true) }
scope :created_before, ->(time) { where("created_at < ?", time) }
end
Event.published.created_before(Time.now)