开发者

How to update records using named scopes in rails

I use this bit of code to search for records using the the like query. Is it possible to create another scope wh开发者_如何转开发ich can update a record.

 scope :search, lambda{|query| where(["name LIKE ?", "%#{query}%"])}


You can use rails' update_all method. See here for a description: http://apidock.com/rails/ActiveRecord/Relation/update_all

EDIT: It's part of the Relation class, so you may call this on scopes (It doesn't say that explicit in the documentation)

EDIT2: It works very much like destroy_all which may also be called on scopes (Relation)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜