开发者

Ruby on Rails 2.3.5: update_all failing on ActiveRecord

I'm trying to update a collection of records in my database using ActiveRecord's update_all. Enter script/console.

MyModel.update_all("reserved = 1", :order => 'rand()', :limit => 1000)

ActiveRecord thinks order is a column, says it's unknown and throws an exception. According to the documentation though, my syntax looks sane. This is RoR 2.3.5.

When doing MyModel.update_all("re开发者_JAVA百科served = 1") alone, it works just fine.

Also if I do

MyModel.update_all("reserved = 1", "reserve_type = 2", :order => "rand()", :limit => 1000) => 0

0 rows affected.

I'm simply trying to do: UPDATE MyModel SET reserved=1, reserve_type=2 ORDER BY RAND() LIMIT 1000


Since the second parameter of update_all should be conditions, you need to pass an empty hash like this:

MyModel.update_all("reserved = 1", {}, {:order => 'rand()', :limit => 1000})

This should work as expected.


Just a clarification on Jakub's response, the correct format should be:

MyModel.update_all("reserved = 1, reserve_type = 2", {}, {:order => 'rand()', :limit => 1000})

If you're updating multiple columns, the first parameter must contain the column list separated by commas.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜