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
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)
There's a couple of ways to update specific fields of a persisted object.
update(attributes)
Updates the attributes of the model from the passed-in hash and saves the record, all wrapped in a transaction. If the object is invalid, the saving will fail and false will be returned.
update!(attributes)
Updates its receiver just like update but calls save! instead of save, so an exception is raised if the record is invalid.
update_attribute(name, value)
Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that
Validation is skipped.
Callbacks are invoked.
updated_at/updated_on column is updated if that column is available.
Updates all the attributes that are dirty in this object.
This method raises an
ActiveRecord::ActiveRecordError
if the attribute is marked as readonly.
update_attributes(attributes)
Alias for
update
update_attributes!(attributes)
Alias for
update!
update_column(name, value)
Equivalent to
update_columns(name => value)
update_columns(attributes)
Updates the attributes directly in the database issuing an UPDATE SQL statement and sets them in the receiver.
This is the fastest way to update attributes because it goes straight to the database, but take into account that in consequence the regular update procedures are totally bypassed. In particular:
Validations are skipped.
Callbacks are skipped.
updated_at/updated_on are not updated.
This method raises an
ActiveRecord::ActiveRecordError
when called on new objects, or when at least one of the attributes is marked as readonly.
update
and update!
(and therefore update_attributes
and update_attributes!
) call save
. So in your situation, you should try using update_column
or update_columns
.
The answer of DanSingerman shows how to update a single field.
But, if the price is a purely calculated value, it should not be a field in your table. But a method in your model:
class House < ActiveRecord::Base
def price
return 100 if colour == "red"
return 200
end
end
(This is obviously a very simplistic method to 'calculate' a price)
Now you can use @house.price
as before and it is dependent on colour, but it is not a table field anymore.
Note: If the calculation is complex, you could 'cache' it in class variable and override the colour=
method to erase the class variable when the colour changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With