我有一个Users类和一个UserGroup类:
class User < ActiveRecord::Base
has_many :group_memberships
has_many :users_groups, through: :group_memberships
...
class UsersGroup < ActiveRecord::Base
has_many :group_memberships
has_many :users, through: :group_memberships
...和一个加入他们的GroupMembership类 -
class GroupMembership < ActiveRecord::Base
belongs_to :users, dependent: :destroy
belongs_to :users_groups, dependent: :destroy
我的迁移看起来像这样 -
class CreateUsersGroups < ActiveRecord::Migration
def change
create_table :users_groups do |t|
t.string :title
t.string :status
t.string :about
t.timestamps null: false
end
end
end
class CreateGroupMembership < ActiveRecord::Migration
def change
create_table :group_memberships do |t|
t.integer :user_id, index: true
t.integer :users_group_id, index: true
t.boolean :owner
end
end
end
所以user.group_memberships
非常高兴,但user.users_groups
会返回错误 -
undefined method `relation_delegate_class' for Users:Module
同样,users_group.group_memberships
很好,但users_group.users
会返回完全相同的错误 -
undefined method `relation_delegate_class' for Users:Module
...在用户模块上。我已经盯着这看了几个小时,但我确定它的语法很简单。有什么问题?
答案 0 :(得分:4)
使用belongs_to时我相信你需要使用单数格式:
所以不是belongs_to :users
而是belongs_to :user
class GroupMembership < ActiveRecord::Base
belongs_to :user, dependent: :destroy
belongs_to :writers_group, dependent: :destroy
答案 1 :(得分:-1)
我相信,因为你正在使用&#39;通过&#39;你将不得不使用:
user.group_memberships.users_groups
这是因为用户没有users_groups,反之亦然。
而是通过group_memberships访问users_groups。