Posts tagged as:

activerecord

Update a single record’s attribute

February 26, 2008

No Gravatar

Need to update a single record’s attribute? This will allow you to update a single attribute and save your record. If you use this method, you will also bypass any validation…beware, attributes can be updated even if the object is invalid. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# find the first Condo
# update the flagged column with the params
 
current_condo = Condo.find(:first)
current_condo.update_attribute(:flagged, params[:submission][:flagged])
 
# find the the condo by permalink
# set the condo.name and condo.zipcode
# save
 
condo = Condo.find_by_permalink("114-pike-street-condos")
condo.name = "114 Pike Street Condos"
condo.zipcode = "98105"
condo.save!

{ Comments }