Sylar
Sylar

Reputation: 12072

Iterating over ActiveRecord_Relation, make changes then save

I have a model, foo.rb. A user can have many "foos". I want to select a specific "foo", make change to their table column then save all at once:

f = Foo.where(bar: 1) # returns, say, four objects.

Now, I want to change: bar: 10, then save:

f.map do |x| x.bar = 10 end

How do you save? I cannot figure this out in irb. Is it possible with less code?

Upvotes: 0

Views: 119

Answers (1)

RAJ
RAJ

Reputation: 9747

You can use update_all:

Foo.where(bar: 1).update_all(bar: 10)

Upvotes: 2

Related Questions