I'd like to use the sliced attribute as a variable and pass it to the update_all argument, but I'm having trouble because I don't know how to write it.

Asked 2 months ago, Updated 2 months ago, 3 views

I would like to update the value of bar with the same attribute name in bulk by specifying the attribute with slice from the Foo object as shown in the code below, but I didn't know how to write attribute as update_all but I would appreciate your advice.

 foo=Foo.first
bar=Bar.where(hoge:hoge)#ActiveRecord::Relation

foo.attributes.slice('a', 'b', 'c') .each do | attribute, value |
  bar.update_all(attribute:value)#I don't know how to write this part...
end

ruby-on-rails

2022-09-30 12:04

1 Answers

How about using update_attributes?

 foo=Foo.first
bars=Bar.where(hoge:hoge)#ActiveRecord::Relation

bars.each { | bar | bar.update_attributes(foo.attributes.slice('a', 'b', 'c')))}


2022-09-30 12:04

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.