开发者

How to update records using named scopes in rails

开发者 https://www.devze.com 2023-02-22 07:30 出处:网络
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.

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

精彩评论

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