0

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

1 Answer 1

1

group_admins returns a collection of GroupAdmin objects. These may correspond (via their user_id) to a user but they aren't actually users so group_admins.include?(user) can only return false.

Depending on what you are doing you could call include? on group_admins.collect {|ga| ga.user} or search for a GroupAdmin with the appropriate user_id value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestions! I will put the working code above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.