In my app, a Person can have a Group, and a Group can be owned by just one person. But persons can be part of groups too, not as owners, but as members. A Group can have more than one member. So to a person become a member, she/he needs to ask for Group_Membership. This way, a group_membership model is created, with a boolean status with false as default. The Group owner than let the member in by changing the status to true. Here are the models:
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"
What I need is to display in the person#show the groups that the person is in, and as well the requests that the person got to the groups owned by him/her.
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
With that I’m able to see the groups that a person is in, even though I set my status in scope as false to test (since status is false by default should show all the requests made by the person). This is the view:
<% @asked_membership.each do |group_membership| %>
<%=h group_membership.member_id %>
How can I can display using this view the name of the groups instead of member_id?
And @asked_group is the right query for me to get the all the requests that were made from other persons to join the group owned by the person in question? If so, (:group_id => params[@group]) is always getting null value and I don’t know how to repair it.
Thanks in advance.
##EDIT##
Changing @asked_group to @asked_group = @person.group_memberships.asked.where(:group_id => @group.id) and adding this to the Group Controller made it work. Now, still need to display name of the groups instead of member_id.
I would rewrite your models as follows:
Now given a
person:You can further optimize the results by eager loading the group and member:
In your view:
To check if a person is a member of a group:
To check if a person is a member of a approved group: