现在我有 3 个模型
Membership:
belongs_to :users
belongs_to :groups
User:
has_many: groups, through: :memberships
accepts_nested_attributes_for :groups
Group:
has_many: users, through: :memberships
然后我在做
u1 = User.new(name: "u1")
u2 = User.new(name: "u2")
g1 = Group.new(name: "1")
u1.groups.find_or_create_by g1.attributes
u2.groups.find_or_create_by g1.attributes
u1.save
u2.save
我期望的是
Groups.all.count == 1
但它总是有'2'。
我该如何解决这个问题?