Here is my model structure:
A User has and belongs to many groups
A Group has many subscriptions
A Group has and belongs to many users
A Group has many GroupAdmins
A Subscription belongs to a group
A GroupAdmin belongs to a user
A GroupAdmin belongs to a Group
When I query the DB (using active record), I should be able to do something like this:
u = User.find 47238
In my User model I have a method called subscriptions:
def subscriptions
self.groups.where(:status => 'active').collect { |group| group.subscriptions.where("subscriptions.status = ? AND subscriptions.expiration_date > ?", 'active', "#{Time.zone.now}").last }.flatten
end
and then to continue with my query:
u.subscriptions.first.group.group_admins
The above returns me a record of a group and the user id I initally queried on.. but for some reason when I run this method inside of Group I get a false returned:
def is_group_admin?(user)
self.group_admins.include? user
end
Working Code:
def is_admin?(user)
self.group_admins.collect { |ga| ga.user }.include? user
end