ruby-on-rails - Rails 依赖 : :destroy causes ArgumentError

标签 ruby-on-rails ruby ruby-on-rails-3 associations foreign-key-relationship

我的 Rails 应用程序中有一个用户模型,如下所示

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>'

我不确定为什么这不起作用 - 我遵循了指南 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

最佳答案

:dependent 不是 has_and_belongs_to_many 的有效选项,因此您需要摆脱它并编写自己的解决方案来满足您的需求。

或者,您可能希望有一个显式连接模型而不是使用 has_and_belongs_to_many。这样您就可以使用 :dependent 来建立与连接模型的关系。

关于ruby-on-rails - Rails 依赖 : :destroy causes ArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10181913/

相关文章:

ruby-on-rails - rails 从 url 中删除 Controller 路径

javascript - 日期选择 onChange 等效/修复?

ruby-on-rails - Ruby 不断给出未知的错误信息

ruby - 从用户输入中查找声明的函数

ruby-on-rails - 强参数 - 无法访问深层嵌套的属性

ruby-on-rails - 哪个 Rails 投票系统 gem 具有以下功能?

ruby-on-rails - rabl View 中的片段缓存?

ruby-on-rails - rails : form_for active record object complaining about no index path

ruby-on-rails - 何时/什么在 Rails 3 中缓存

Mysql::错误:表 'table_name' 被标记为崩溃,应该修复