3

Is possible something like:

@users = User.criteria.for_ids(params[:user_ids])
@users.update_all(:suspend => true)

instead of:

for u in @users
 u.suspend = true
 u.update_attributes
end

2 Answers 2

5

Take a look at this:

# Updating one record:
Person.update(15, :user_name => 'Samuel', :group => 'expert')

# Updating multiple records:
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)

Seen here: http://apidock.com/rails/ActiveRecord/Base/update/class

Updates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.

In your case:

User.update(params[:user_ids], :suspend => true)

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Arf, didn't take attention to that, sorry ... :/
3

As long as for_ids method returns a criteria(instead of an array), you can use update_all.

@users = User.criteria.for_ids(params[:user_ids])
@users.update_all(:suspend => true)

The update_all call gets translated in to a set call. Refer to the documentation for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.