What is the proper way to update a record in Ruby on Rails? Just a single record. For example, I want to modify the name of the title somehow. A code snippet would be awesome!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The basic way to update a record is like that :
this assumes that
@userin an instance of aUserclass having anamefield.you can also do mass_assignment with the
update_attributesmethodIf you want an exception to be raised if the record is invalid you can use the bang method instead, so here it would be
save!&update_attributes!You can also use the
update_attributewhich has been (or will be soon) renamed toupdate_columnto update a single column while skipping the validation, but you should generally avoid using this methodmore doc there
Finally you can use the write_attribute method