Search code examples
ruby-on-rails-4rakerails-activerecordrails-migrations

Edit property of all elements in a database table


I have a database with 30 - 50 users and I'd like to edit a property in everyone of them.

I'm not well versed in the arts of SQL but I think a for or some sort of loop would work.

Instead of having to write :

u = User.find(id)
u.air = true
u.save

What could I write?


Solution

  • If you want them to all be updated in the same way, try:

    User.update_all(air: true)
    

    If you require different updates for each, try:

    User.all.each do |u|
        u.air = true
        # add whatever other logic you need here
        u.save
    end