Rails - 视图上的模型关联

发布于 2024-11-08 05:24:59 字数 1250 浏览 0 评论 0原文

我有这样的模型:

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页面上可以看到谁申请了成员资格以及群组他参加了。由于 groupsgroup_memberships belongs_toperson,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2024-11-15 05:24:59

这里有一些伪代码,我认为它们会让您走上您想要的道路: https://gist.github.com /981513

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

    scope :owned_groups, where(:is_owner => true).joins(:group_memberships) # gets all groups where this person is owner
end

class Group_Membership
    belongs_to :member, :class_name => 'Person'
    belongs_to :group

    # note that these attributes need to be defined
    # is_owner (boolean)
    # member_approved (boolean)

    scope :requested, :where(:member_approved => false)
end

class Group
    belongs_to :person
    has_many :group_memberships
    has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"
end

公平警告,我根本没有测试过它,而且我仍在学习新的 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

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

    scope :owned_groups, where(:is_owner => true).joins(:group_memberships) # gets all groups where this person is owner
end

class Group_Membership
    belongs_to :member, :class_name => 'Person'
    belongs_to :group

    # note that these attributes need to be defined
    # is_owner (boolean)
    # member_approved (boolean)

    scope :requested, :where(:member_approved => false)
end

class Group
    belongs_to :person
    has_many :group_memberships
    has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id"
end

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.

甲如呢乙后呢 2024-11-15 05:24:59

我是这样想的

class Person
  has_many :groups
  has_many :group_memberships, :foreign_key => "member_id"
  has_many :own_groups, :class_name => "Group", :through => "group_memberships", :foreign_key => "group_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 :asked_group_memberships, :class_name => 'Group_Membership', :conditions => ['status = ?', false]
  has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id", :conditions => ['group_memberships.status = ?', true]
end

I think like that

class Person
  has_many :groups
  has_many :group_memberships, :foreign_key => "member_id"
  has_many :own_groups, :class_name => "Group", :through => "group_memberships", :foreign_key => "group_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 :asked_group_memberships, :class_name => 'Group_Membership', :conditions => ['status = ?', false]
  has_many :members, :class_name => "Person", :through => "group_memberships", :foreign_key => "member_id", :conditions => ['group_memberships.status = ?', true]
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文