Vekka
Vekka

Reputation: 141

Update_all using object attribute

I'm interested if it is possible to use objects attribute as value in update_all method.

Let's say we have relation Article and it has attributes part1 and part2. Now I want to update attribute part1 of all objects in relation Article with value of part2.

Article.update_all(part1: current_object.part2)

Where current_object i just used for representation of currently updated object.

I know that I can do something like that

Article.find_each { |article| article.update(part1: article.part2) }

But I wanted to know if this is achievable with update_all method.

Upvotes: 2

Views: 1201

Answers (1)

marmeladze
marmeladze

Reputation: 6564

Below code will do it. But why are you doing this? -))

Article.update_all("part1 = part2")

Upvotes: 6

Related Questions