I have a Purchase model which has_one
Coupon.
The Purchase model contains this code:
def decrement_coupon
coupon = Coupon.find_by_code(coupon_code, :readonly => false)
return unless coupon.respond_to?(:uses)
coupon.uses = coupon.uses - 1
coupon.save
end
But I got this error the other day:
ActiveRecord::ReadOnlyRecord - ActiveRecord::ReadOnlyRecord:
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/persistence.rb:245:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/callbacks.rb:273:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activesupport-3.0.6/lib/active_support/callbacks.rb:419:in `_run_save_callbacks'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/callbacks.rb:273:in `create_or_update'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/persistence.rb:39:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/validations.rb:43:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:240:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:207:in `transaction'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:240:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:251:in `rollback_active_record_state!'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/transactions.rb:239:in `save'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/associations/association_proxy.rb:222:in `send'
/usr/local/rvm/gems/ree-1.8.7-2010.02@lyconic/gems/activerecord-3.0.6/lib/active_record/associations/association_proxy.rb:222:in `method_missing'
/var/www/88tactical_production/releases/20110823232510/app/models/purchase.rb:69:in `decrement_coupon'
Does :readonly => false
not guarantee a non-readonly record? Would moving this function to the Coupon model make a difference? (I'll probably do that regardless)
This is probably due to joining in other child values through your find_by_code() helper.
When ActiveRecord uses joins to lookup an object returning additional values, it marks the record read-only.
There's a thorough description of this on this question here:
What is causing this ActiveRecord::ReadOnlyRecord error?
If this is the case, you can probably fix the issue by turning a your :joins into a proper :include that populates the ActiveRecord instances without marking them read-only.
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