Rails - 无法进行正确的查询

时间:2011-05-21 20:11:44

标签: ruby-on-rails ruby-on-rails-3 models

在我的应用中,Person可以拥有Group,而一个群组只能由一个人拥有。但是,人们也可以成为团体的一部分,不是作为所有者,而是作为成员。一个集团可以拥有多个成员。因此,对于一个人成为会员,她/他需要要求Group_Membership。这样,就会创建一个group_membership模型,其中boolean status默认为false。群组所有者将status更改为true,然后让该成员进入。以下是模型:

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
scope :asked, where(:status => false)
end

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

我需要的是在person#show中显示该人所在的群组,以及该人对他/她所拥有的群体的要求。

def show
@person = Person.find(params[:id])
@asked_membership = @person.group_memberships.asked.where(:member_id => params[:id]) 
@group = @person.groups.where(:person_id => params[:id])
@asked_group = @person.group_memberships.asked.where(:group_id => params[@group])
end

通过这个我能够看到一个人所在的群组,即使我将status中的scope设置为false来进行测试(因为默认情况下状态为false应显示所有人提出的要求)。这是观点:

    <% @asked_membership.each do |group_membership| %>
    <%=h group_membership.member_id %> 

如何使用此视图显示组的名称而不是member_id

@asked_group对我来说是正确的查询,可以获取其他人加入相关人员拥有的群组的所有请求吗?如果是这样,(:group_id => params[@group])总是得到空值,我不知道如何修复它。

提前致谢。

## EDIT ##

@asked_group更改为@asked_group = @person.group_memberships.asked.where(:group_id => @group.id)并将其添加到Group Controller即可。现在,仍然需要显示组的名称而不是member_id

1 个答案:

答案 0 :(得分:2)

我会按如下方式重写你的模型:

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

  has_many :approved_groups, :through => :group_memberships, :source => :group, 
               :conditions => [ "group_memberships.status = ? ", true]

  has_many :applied_groups, :through => :group_memberships,, :source => :group, 
               :conditions => [ "group_memberships.status = ? ", false]

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

  has_many :approved_members,:through => :group_memberships, :source => :member, 
               :conditions => [ "group_memberships.status = ? ", true]

  has_many :applied_members,:through => :group_memberships, :source => :member, 
               :conditions => [ "group_memberships.status = ? ", false]

end

现在给出person

# returns all the groups is user is member of
person.groups

# returns all the groups owned by the user                  
person.owned_groups                

# returns the memberships of all the groups owned by the user
person.owned_group_memberships 

# returns the memberships requests for all the groups owned by the user
person.owned_group_memberships.find_all_by_status(false)

您可以通过加载群组和成员来进一步优化结果:

@asked_group_memberships=person.owned_group_memberships.find_all_by_status(false,
  :include => [:group, :member])

在您看来:

<% @asked_group_memberships.each do | agm| %>
  <p> 
      Requester: <%= agm.member.name %>, 
      Group: <%=agm.group.name%> 
  </p>
<%end %>

检查某人是否是某个群组的成员:

person.groups.exists?(group)

检查某人是否是经批准的群组的成员:

person.approved_groups.exists?(group)