Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use update_attributes in rails 4?

Currently I am upgrading my application from rails 3.2 to rails 4. Got the following error.

ArgumentError (argument out of range):

This error occured because of following line of code

@lease.update_attributes(params[:lease])

I tried by update that also throw the same error. Is update_attributes removed from rails 4.how to use this?

code:

def terms_build_methods
  if @lease.blank?
    @lease = params[:current_lease_id].present? ? Lease.find_by_id(params[:current_lease_id].to_i) : Lease.create(params[:lease])
  else
    @lease.update(lease_params)
  end
  Lease.update_lease_occupancy_type(@lease)
  Lease.update_lease_status(@lease)
end

private

def lease_params
  params.require(:lease).permit!
end
like image 851
kannathasan Avatar asked Nov 26 '13 10:11

kannathasan


People also ask

What does update_attributes do?

This method used to be called update_attributes in Rails 3. It changes the attributes of the model, checks the validations, and updates the record in the database if it validates. Note that just like update_attribute this method also saves other changed attributes to the database.

Does update_attributes call save?

Use update_attribute to skip validations. update_attribute uses save(false) while update_attributes uses save (which means save(true)). If perform_validation is false while calling save then it skips validation, and it also means that all the before_* callbacks associated with save.

How do you update attributes in Ruby on Rails?

Use update_attribute to change an attribute and persist it without running validations. Use update to change an attribute, check the validations and persist the change if validations pass. You can find an object and update it with a one command using update as class method.


1 Answers

Just use @lease.update(lease_params) this and also in the controller add a private method

def lease_params
  params.require(:lease).permit!
end
like image 132
Sabyasachi Ghosh Avatar answered Oct 29 '22 16:10

Sabyasachi Ghosh