Monday, February 15, 2010

ActiveRecord::ReadOnlyRecord while updating object fetched by joins

by Sandip Ransing 0 comments

ActiveRecord find with join options retrieves object as readonly
station = Station.find( :first, :joins => :call, :conditions => ["customer_id = ? and date(insurance_expiry_date) = ?", customer.id, insurance_expiry_date ] )
Readonly object cannot modified and hence below line raises "ActiveRecord::ReadOnlyRecord" error.
station.update_attributes({ :customer_id => 12 })
If you have to write on read only object then you can pass following option to find query
:readonly => false
Now below find is permitted to do write on fetched object records.
station = Station.find( :first, :joins => :call, :conditions => ["customer_id = ? and date(insurance_expiry_date) = ?", customer.id, insurance_expiry_date ], :readonly => false )

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter