ActiveRecord::ReadOnlyRecord while updating object fetched by joins

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. 

station.update_attributes({ :customer_id => 12 })

should raise following error.

ActiveRecord::ReadOnlyRecord

If you wanted to do write on object then you need to pass following option to find query.

:readonly => false

station = Station.find( :first, :joins => :call, :conditions => ["customer_id = ? and date(insurance_expiry_date) = ?", customer.id, insurance_expiry_date ], :readonly => false )

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.