Rails:多态关联(has_many)异常

时间:2019-04-04 06:33:11

标签: ruby-on-rails ruby-on-rails-4 polymorphic-associations

尝试a具有很多关联,其中联接表(UserGroupMembers)是多态的。这是在Rails 4上

我该如何解决?

在创建具有成员的组时,发生例外。即联接表错误

User group memberships is invalid

代码与用户创建群组

group = Group.new(name: "test")
group.users << User.first
group.valid? (or group.save)
class Group < ActiveRecord::Base
  has_many :user_group_members, as: :link
  has_many :users, through: :user_group_members, autosave: true
end
class User < ActiveRecord::Base
  has_many :user_group_members
  has_many :groups , through: :user_group_members, source: :link, source_type: :"Group"
end
class UserGroupMembers < ActiveRecord::Base
  belongs_to :link , polymorphic: :true,required: true
  belongs_to :user, required: true
end

0 个答案:

没有答案