gpt4 book ai didi

ruby-on-rails - 这段代码如何抛出 ActiveRecord::ReadOnlyRecord 异常?

转载 作者:行者123 更新时间:2023-12-04 22:35:40 26 4
gpt4 key购买 nike

我有一个 购买 型号has_one 优惠券 .

购买 模型包含此代码:

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

但前几天我收到了这个错误:
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'

是否 :readonly => false不保证非只读记录?将此功能移至 优惠券 型号有区别吗? (无论如何我可能会这样做)

最佳答案

这可能是由于通过您的 find_by_code() 助手加入了其他子值。

当 ActiveRecord 使用连接查找返回附加值的对象时,它会将记录标记为只读。

这里有关于这个问题的详尽描述:

What is causing this ActiveRecord::ReadOnlyRecord error?

如果是这种情况,您可能可以通过打开 来解决问题。 :joins 成合适的 :包括 填充 ActiveRecord 实例而不将它们标记为只读。

关于ruby-on-rails - 这段代码如何抛出 ActiveRecord::ReadOnlyRecord 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com