11155
(-- --)
February 21, 2012, 3:25pm
1
Hi,
I am trying to use the update method of activerecord with an array of
ids as the first param.
It does not update any row. The APIs say that it should work.
The array contains 2 IDs.
RSCampaign.update([1,2], {:end_date =>
Date.strptime("120228","%y%m%d")})
If I put an ID instead of the array, it updates fine.
Please help.
I am using ruby 1.8.6
What error message do you get? Do both of those IDs map to records? Do either
of them get updated?
11155
(-- --)
February 21, 2012, 5:30pm
3
Hey,
I get no error message.
It just doesn't update any record.
Both the IDs are there in the DB table.
API says that exactly:
RSCampaign.update([1,2], {:end_date =>[Date.strptime("120228","%y%m%d")}]*2)
sorry
RSCampaign.update([1,2], [{:end_date =>Date.strptime("120228","%y%m%d")}]*2)
11155
(-- --)
February 21, 2012, 7:42pm
6
Thank you, Valery.
You are my hero.
Can you please post a link to the APIs?
api.rubyonrails.org
or
https://github.com/rails/rails/blob/d22592a05b299c21e30ec8b38890a178dca863b4/activerecord/lib/active_record/relation.rb#L312
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)
people is a Hash
people.keys is an Array of keys # [1,2]
people.values is an Array of values # [{ “first_name” => “David” }, { “first_name” => “Jeremy”}]