不能破坏多对多关系中的记录 [英] Can't destroy record in many-to-many relationship
问题描述
我是Rails的新手,所以我确定我犯了一个简单的错误.
I'm new to Rails, so I'm sure I've made a simple mistake.
我已经在两个模型之间建立了多对多关系:User
和Group
.它们通过结点模型GroupMember
连接起来.
I've set up a many-to-many relationship between two models: User
and Group
. They're connected through the junction model GroupMember
.
这是我的模型(删除了无关的内容):
Here are my models (removed irrelevant stuff):
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"指南).
The table for GroupMembers
contains additional information about the relationship, so I didn't use has_and_belongs_to_many
(as per the Rails "Active Record Associations" guide).
我遇到的问题是我无法销毁GroupMember
.
The problem I'm having is that I can't destroy a GroupMember
.
这是Rails控制台的输出:
Here's the output from 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
这让我发疯,所以任何帮助将不胜感激.
This is driving me crazy, so any help would be greatly appreciated.
推荐答案
当您从HABTM关系切换为has_many:through关系时,您似乎忘记了添加id列.活动记录需要GroupMember具有.destroy的ID才能正常工作.
When you switched from the HABTM relationship to a has_many :through relationship it looks like you may have forgotten to add back the id column. Active record needs GroupMember to have an id for .destroy to work like that.
在迁移中查找:id => false
并摆脱它.然后重做迁移.
Look for the :id => false
in your migration and get rid of it. then redo the migration.
希望有帮助.
这篇关于不能破坏多对多关系中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!