Rails-模型怀疑 [英] Rails - Model Doubt

查看:273
本文介绍了Rails-模型怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我拥有这样的模型:

Given the fact that I have models like this:

class Person
  has_many :owned_groups, :class_name => "Group", :foreign_key => :owner_id
  has_many :owned_group_memberships, :through => :owned_groups, 
              :source => :group_memberships 

  has_many :group_memberships, :foreign_key => "member_id"
  has_many :groups, :through => :group_memberships

end

class GroupMembership
  belongs_to :member, :class_name => 'Person'
  belongs_to :group
end

class Group
  belongs_to :owner, :class_name => "Person"
  has_many :group_memberships
  has_many :members, :through => :group_memberships
end

如何访问groupmembers?总是我做@group.members或类似的事情,给我一个错误,指出在模型中找不到该关系.

How can I access the members a group has? Always I do @group.members or things like that, gives me an error saying that the relation can't be found in the model.

谢谢.

## EDIT ##

我得到的错误是:Could not find the association "group_memberships" in model Group

推荐答案

您可能需要在has_many:members语句中指定:class_name =>'Person'.

You may need to specify :class_name => 'Person' in your has_many :members statement.

这篇关于Rails-模型怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆