How can I use a named scope in my model against an array of items?

后端 未结 2 1524
渐次进展
渐次进展 2021-01-05 12:52

I know that I can do a query for recent books based on an array as in

scope :recent_books, lambda {|since_dt| {:conditions=>{:created_at >= since_dt}}}

2条回答
  •  执念已碎
    2021-01-05 13:12

    In Rails 4, I can check for the inclusion of a string in an array attribute by using a scope like this:

    scope :news, -> { where(:categories => '{news}') }
    

    Or with an argument:

    scope :by_category, ->(category) { where(:categories => "{#{category}}") }
    

提交回复
热议问题