update-all

With Mongoid, can I “update_all” to push a value onto an array field for multiple entries at once?

狂风中的少年 提交于 2019-12-23 09:41:42
问题 Using Mongoid, is it possible to use "update_all" to push a value onto an array field for all entries matching a certain criteria? Example: class Foo field :username field :bar, :type => Array def update_all_bars array_of_names = ['foo','bar','baz'] Foo.any_in(username: foo).each do |f| f.push(:bar,'my_new_val') end end end I'm wondering if there's a way to update all the users at once (to push the value 'my_new_val' onto the "foo" field for each matching entry) using "update_all" (or

CakePHP increment value

别来无恙 提交于 2019-12-20 04:07:17
问题 My problem looks as follows: I want to make a vote app where People can choose one or more Events(like Doodle). For this I have set up a function called vote. In the View you can choose the Events using checkboxes. The Models are Poll and Groupevent. A Poll has Many Groupevents. My Problem is when I call updateAll() , all values of the associated Groupevents are incremented. Here is my code: View: echo $this->Form->create('Poll', array('action' => 'vote')); for($i=0; $i<$count; $i++){ echo

CakePHP increment value

非 Y 不嫁゛ 提交于 2019-12-02 06:14:21
My problem looks as follows: I want to make a vote app where People can choose one or more Events(like Doodle). For this I have set up a function called vote. In the View you can choose the Events using checkboxes. The Models are Poll and Groupevent. A Poll has Many Groupevents. My Problem is when I call updateAll() , all values of the associated Groupevents are incremented. Here is my code: View: echo $this->Form->create('Poll', array('action' => 'vote')); for($i=0; $i<$count; $i++){ echo $this->Form->input('Groupevent.'.$i.'.votes', array('type'=>'checkbox', 'label'=>$this->Groupevent[

How to update all when you need callbacks fired?

▼魔方 西西 提交于 2019-11-29 11:07:52
问题 Let's say I've got 15 user ids in an array called user_ids . If I want to, say, change all of their names to "Bob" I could do: users = User.find(user_ids) users.update_all( :name => 'Bob' ) This doesn't trigger callbacks, though. If I need to trigger callbacks on these records saving, to my knowledge the only way is to use: users = User.find(user_ids) users.each do |u| u.name = 'Bob' u.save end This potentially means a very long running task in a controller action, however. So, my question is