ruby-on-railsruby

Ruby on rails save a specific field in database


I have a table in my database which is house, that has many field like "colour", "price". How can I save only a specific field after updating

if I have this,

@house.colour = newcolour
@house.save

it will save all other field including house.colour, and house.price
(my case, price should be programmatically updated at the same time with the colour, but should not be saved. only house.colour should be saved in the database)

i tried to do

@house.colour = newcolour
@house.colour.save

but it shows me error

Is it possible only to save only the value of @house.colour ?

Thank you for any suggestion


Solution

  • You can update a precise set of fields with update_attributes.

    @house.update_attributes(:colour => newcolour)
    

    You can update a single field with update_attribute.

    @house.update_attribute(:colour, newcolour)