美文网首页
model-scope

model-scope

作者: 栋栋晓 | 来源:发表于2016-01-18 02:03 被阅读21次

scope
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)

相关文章

  • model-scope

    scopescope 的作用就是將時常使用或是複雜的ORM語法組合成懶人包,這樣下次要用的時候只要把懶人包拿出來就...

  • model-scope

    scope scope 的作用就是將時常使用或是複雜的ORM語法組合成懶人包,這樣下次要用的時候只要把懶人包拿出來...

网友评论

      本文标题:model-scope

      本文链接:https://www.haomeiwen.com/subject/wmrkkttx.html