无法在多对多关系中销毁记录

时间:2011-02-22 23:23:58

标签: ruby-on-rails model destroy

我是Rails的新手,所以我确信我犯了一个简单的错误。

我在两个模型之间建立了多对多关系:UserGroup。它们通过联结模型GroupMember连接。

以下是我的模型(删除不相关的内容):

class User < ActiveRecord::Base
  has_many :group_members
  has_many :groups, :through => :group_members
end

class GroupMember < ActiveRecord::Base
  belongs_to :group
  belongs_to :user
end

class Group < ActiveRecord::Base
  has_many :group_members
  has_many :users, :through => :group_members
end

GroupMembers的表包含有关关系的其他信息,因此我没有使用has_and_belongs_to_many(根据Rails“Active Record Associations”指南)。

我遇到的问题是我无法销毁GroupMember

以下是rails console的输出:

irb(main):006:0> m = GroupMember.new
=> #<GroupMember group_id: nil, user_id: nil, active: nil, created_at: nil, updated_at: nil>
irb(main):007:0> m.group_id =1
=> 1
irb(main):008:0> m.user_id = 16
=> 16
irb(main):009:0> m.save
=> true
irb(main):010:0> m.destroy
NoMethodError: undefined method `eq' for nil:NilClass
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.4/lib/active_support/whiny_nil.rb:48:in `method_missing'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/persistence.rb:79:in `destroy'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/locking/optimistic.rb:110:in `destroy'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/callbacks.rb:260:in `destroy'
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.4/lib/active_support/callbacks.rb:413:in `_run_destroy_callbacks'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/callbacks.rb:260:in `destroy'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/transactions.rb:235:in `destroy'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/transactions.rb:292:in `with_transaction_returning_status'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/transactions.rb:207:in `transaction'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
    from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.4/lib/active_record/transactions.rb:235:in `destroy'
    from (irb):10

这让我发疯,所以任何帮助都会非常感激。

5 个答案:

答案 0 :(得分:5)

当您从HABTM关系切换到has_many:through关系时,您可能忘记添加回id列。活动记录需要GroupMember具有.destroy的id才能像这样工作。

在迁移中查找:id => false并删除它。然后重做迁移。

希望有所帮助。

答案 1 :(得分:0)

您是否尝试过m.reload.destroy代替m.destroy,看看会发生什么?

答案 2 :(得分:0)

我认为数据库中的group_members表没有主键。也许你没有设置它。因此,当您创建一个新的GroupMember对象并保存它时,它将没有id。您无法销毁没有id(主键)的对象。

答案 3 :(得分:0)

我遇到了你所描述的同样的问题,你的帖子让我进一步寻找解决方案。这是我发现的:

1-从http://compositekeys.rubyforge.org/获取此复合主键库 2-将其添加到您的gem文件中 3-将以下内容添加到GroupMember    set_primary_keys [:user_id,:group_id]

  • 注意set_primary_keys而不是set_primary_key

当您调用GroupMember.primary_key时,这将返回正确的主要内容 当destroy查找.primary_key时,它现在会找到它。

在您的控制台中尝试: gm = GroupMember.first gm.destroy

我希望这有助于=)

答案 4 :(得分:0)

如果您使用关联并且出于任何原因选择不使用Active Records默认ID,则可以使用“set_primary_key”设置默认ID,以便查找和销毁等方法正常工作。

class User < ActiveRecord::Base
  set_primary_key :user_primary
  has_many :group_members, :dependent => :destroy
  has_many :groups, :through => :group_members
end

class GroupMember < ActiveRecord::Base
  set_primary_key :group_member_primary
  belongs_to :group
  belongs_to :user
end

class Group < ActiveRecord::Base
  set_primary_key :group_primary
  has_many :group_members, :dependent => :destroy
  has_many :users, :through => :group_members
end