Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update specific column in a activerecord on rails?

I have a model called Agency

One of the row from Agency active record looks as follows:

#<Agency id: 1, name: "Hello", contact_person: "Abhimanyu", phone_number: "946159", email: "[email protected] ", local_package: false, outstation: true, remarks: nil, status: 1>

Here I want to change the value of status to 0 . How can I do that ?

like image 964
user3576036 Avatar asked Jul 22 '17 09:07

user3576036


2 Answers

Rails provides many method for update an activerecord. You can get differences of each method from this blog

agency = Agency.find(1)
  1. update

    agency.update(status: 0)
    
  2. update_attribute

    agency.update_attribute(:status, 0)
    
  3. update_column

    agency.update_column(:status, 0)
    
  4. update_columns

    agency.update_columns(status: 0)
    
like image 103
Sonu Kumar Avatar answered Sep 19 '22 19:09

Sonu Kumar


You can also use this

Agency.find(1).update_column(:status, 0)

like image 42
Deepak Choudhary Avatar answered Sep 18 '22 19:09

Deepak Choudhary