我在轨用户模式应用程序如下Rails的依赖:摧毁的原因引发ArgumentError
class User < ActiveRecord::Base
has_many :parts, dependent: :destroy
has_many :assemblies, dependent: :destroy
has_many :packages, dependent: :destroy
has_many :manufacturers, dependent: :destroy
//more code here
end
我也有一些其他车型dependent: :restrict
。
当我运行在rails console
下,我得到一个ArgumentError:
u = User.first
u.parts
到u.parts
的调用提供了以下错误信息:
ArgumentError: Unknown key: dependent
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/core_ext/hash/keys.rb:44:in `block in assert_valid_keys'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/core_ext/hash/keys.rb:43:in `each_key'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/builder/association.rb:33:in `validate_options'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/builder/association.rb:24:in `build'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/builder/collection_association.rb:23:in `build'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/autosave_association.rb:139:in `build'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/builder/has_and_belongs_to_many.rb:8:in `build'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/builder/collection_association.rb:13:in `build'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations.rb:1600:in `has_and_belongs_to_many'
from /home/david/code/PartSorter/app/models/part.rb:5:in `<class:Part>'
from /home/david/code/PartSorter/app/models/part.rb:1:in `<top (required)>'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:469:in `load'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:469:in `block in load_file'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:468:in `load_file'
... 12 levels...
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:554:in `get'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:588:in `constantize'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/inheritance.rb:111:in `block in compute_type'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/inheritance.rb:109:in `each'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/inheritance.rb:109:in `compute_type'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/reflection.rb:172:in `klass'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/association.rb:117:in `klass'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/association.rb:165:in `find_target?'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:332:in `load_target'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/associations/collection_proxy.rb:88:in `method_missing'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /home/david/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
我不知道为什么,这ISN” t工作 - 我按照指南here添加dependent
部分,但它不工作。有任何想法吗?
编辑:是 的part.rb的内容如下:
class Part < ActiveRecord::Base
has_many :part_packages
has_many :packages, through: :part_packages
belongs_to :manufacturer
has_and_belongs_to_many :assemblies, :dependent => :restrict
belongs_to :user
validates :name, :user_id, :presence => true
def quantity
@quantity = 0
self.part_packages.each do |pp|
@quantity += pp.quantity
end
@quantity
end
end
错误出现在'app/models/part.rb'中,所以请提供代码而不是'user.rb'。 – 2012-04-16 21:37:09
我已经添加了上面的源代码。 – 2012-04-16 21:41:30
当您从控制台尝试“Part.first”和“u.parts.size”时会发生什么? (缩小范围) – IAmNaN 2012-04-16 21:50:31