Rails - 视图上的模型关联
我有这样的模型:
class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end
class Group_Membership
belongs_to :member, :class_name => 'Person'
belongs_to :group
end
class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"
一个人请求会员资格
,创建一个status false
的模型。 群组
的所有者在他的person#show
页面上可以看到谁申请了成员资格
以及群组
他参加了。由于 groups
和 group_memberships
belongs_to
是 person
,但 group_membership
也是 belongs_to
一个 group
,我如何在 person#show
中创建一个视图,允许 person
查看谁请求了 会员资格
person
拥有哪些 group
,以及 person
拥有哪些 group
成员资格?
##EDIT##
这里的status
默认为false
,因为person
只会在< code>group 如果所有者编辑
此状态并将其设置为true
。
想法取自这个问题:关于 Rails 上的关联和模型的问题
I have models like that:
class Person
has_many :groups
has_many :group_memberships, :foreign_key => "member_id"
end
class Group_Membership
belongs_to :member, :class_name => 'Person'
belongs_to :group
end
class Group
belongs_to :person
has_many :group_memberships
has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"
A person asks for a membership
, creating a model of it with status false
. The owner of the group
on his person#show
page can see who have asked for membership
and also the groups
he takes part. Since groups
and group_memberships
belongs_to
a person
, but group_membership
also belongs_to
a group
, how can I make a view in person#show
that allows the person
to see who have asked for memberships
on the groups
the person
own, and also what groups
the person
has a membership
?
##EDIT##
The status
here is false
as default because a person
will just be accepted in the group
if the owner edit
this status and set it as true
.
Idea taken from this question: Question about Association and Models on Rails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些伪代码,我认为它们会让您走上您想要的道路: https://gist.github.com /981513
公平警告,我根本没有测试过它,而且我仍在学习新的 AR 模式:)
我认为你的 group_memberships 关系可能最适合作为 :through 关系,然后创建范围围绕关系可能具有的不同“状态”。您也可以查看 状态机 以获得一些帮助。
Here's some psuedo code that I think will get you on the path to what you want: https://gist.github.com/981513
Fair warning, I haven't tested it at all, and I'm still learning the new AR patterns :)
I think that your group_memberships relation is probably a best fit as a :through relationship and then creating scopes around the different "states" that relationship can have. You might checkout state machine for some help on this too.
我是这样想的
I think like that