# Rails Grape API Endpoint optional params validation in update

I have the following rails grape api endpoint in the users resource:

    desc 'updates a user'
params do
optional :first_name, type: String
optional :active, type: Boolean
optional :second_name, type: String
optional :rating, type: Integer
requires :id, type: Integer
end
put ':id' do
user = User.find_or_create_by(id: params[:id])
user.name = params[:name] if params[:name].present?
user.active = params[:active] if params[:active].present?
user.second_name = params[:second_name] if params[:second_name].present?
user.rating = params[:rating] if params[:actratingve].present?
user.save!
end


Any nicer way to update model without asking per line if the param is present?

user.assign_attributes(params)

• I did upvote your answer, but you could add a bit of text to add a bit of meat to your answer. This is not Stakc Overlfow, we really to help OP and others readers to improve. You probably should mention what assign_attributes so reader can have it directly in your answer do not needs to read the doc. Jun 28, 2016 at 15:09