基于此问题/答案:NoMethodError - new since upgrading to Rails 4
这是/models/product.rb的相关部分
class Product < ActiveRecord::Base
delegate_attributes :is_master, :to => :master
has_one :master,
-> { where(["variants.is_master = ? AND variants.deleted_at IS NULL", true]) },
:class_name => 'Variant',
:autosave => true
accepts_nested_attributes_for :master, allow_destroy: false
这是/models/variant.rb:
class Variant < ActiveRecord::Base
belongs_to :product
scope :master, -> { where(["variants.is_master = ? AND variants.deleted_at IS NULL", true]) }
当前我收到此错误:
NoMethodError: undefined method `is_master=' for #<Product:0x0000000d63b980>
在升级到Rails 4之前,这曾经不是问题,因为proxy_attributes应该处理它。不赞成使用gem gem_attributes,但方法已移至我已安装的active_tools中。但是显然有些东西仍然坏了,所以我必须解决它。
我通过以下方法解决了类似的价格问题:
def price=(amount)
master.price = amount
end
但是当我尝试对is_master执行相同操作时:
def is_master=(bool)
master.is_master = bool
end
我得到一个错误stack level too deep.
,如果master.is_master反复调用is_master方法,我可以理解为什么,但是我想知道为什么这不是价格问题。
这是在/models/product.rb中调用is_master
的地方,但不在Class Products
内部。
module ProductConcerns
module Callbacks
extend ActiveSupport::Concern
included do
before_create :set_master_variant_defaults
end
def set_master_variant_defaults
self.is_master = true
end
end
end
堆栈跟踪:
undefined method `is_master=' for #<Product:0x0000000c3bb378>
/pwd/vendor/ruby/2.1.0/gems/activemodel-4.2.10/lib/active_model/attribute_methods.rb:433:in `method_missing'
/pwd/app/models/product.rb:874:in `set_master_variant_defaults'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:432:in `block in make_lambda'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:164:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:164:in `block in halting'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:504:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:504:in `block in call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:504:in `each'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:504:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:778:in `_run_create_callbacks'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/callbacks.rb:306:in `_create_record'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/timestamp.rb:57:in `_create_record'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/persistence.rb:504:in `create_or_update'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:117:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:117:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:505:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:505:in `call'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/callbacks.rb:302:in `create_or_update'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/persistence.rb:142:in `save!'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/validations.rb:43:in `save!'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:291:in `block in save!'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:220:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:291:in `save!'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:487:in `block in save!'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:502:in `block (2 levels) in around_save'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:150:in `block in run_actions'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:170:in `catch_exceptions'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:148:in `run_actions'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:133:in `run_callbacks'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:212:in `run_callbacks'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:63:in `block (2 levels) in perform'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:63:in `catch'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:63:in `block in perform'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:186:in `within_transaction'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/transition_collection.rb:62:in `perform'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:502:in `block in around_save'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:530:in `block in transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:220:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:529:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:501:in `around_save'
/pwd/vendor/ruby/2.1.0/gems/state_machine-1.2.0/lib/state_machine/integrations/active_record.rb:487:in `save!'
/pwd/vendor/ruby/2.1.0/gems/protected_attributes-1.1.4/lib/active_record/mass_assignment_security/validations.rb:17:in `create!'
/pwd/db/seeds.rb:112:in `block in create_merchant_with_listed_product'
/pwd/db/seeds.rb:110:in `times'
/pwd/db/seeds.rb:110:in `create_merchant_with_listed_product'
/pwd/db/seeds.rb:201:in `block in <top (required)>'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/transactions.rb:220:in `transaction'
/pwd/db/seeds.rb:8:in `<top (required)>'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `load'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `block in load'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:240:in `load_dependency'
/pwd/vendor/ruby/2.1.0/gems/activesupport-4.2.10/lib/active_support/dependencies.rb:268:in `load'
/pwd/vendor/ruby/2.1.0/gems/railties-4.2.10/lib/rails/engine.rb:547:in `load_seed'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/tasks/database_tasks.rb:253:in `load_seed'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
/pwd/vendor/ruby/2.1.0/gems/activerecord-4.2.10/lib/active_record/railties/databases.rake:132:in `block (2 levels) in <top (required)>'