Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show all users with a particular badge? #56

Closed
Shpigford opened this issue Feb 2, 2013 · 4 comments
Closed

Show all users with a particular badge? #56

Shpigford opened this issue Feb 2, 2013 · 4 comments

Comments

@Shpigford
Copy link

Is there a way to list all the users/records that have been awarded a particular badge?

Something like: Badge.find(2).users

@tute
Copy link
Member

tute commented Feb 2, 2013

Hi! Try:

def users_with_badge_id(badge_id)
  sashes_with_badge = BadgesSash.where(badge_id: badge_id).pluck(:sash_id)
  User.where(sash_id: sashes_with_badge)
end
users_array = users_with_badge_id(1)

Thanks for your feedback.

@tute
Copy link
Member

tute commented Feb 2, 2013

You could also add in config/initializers/merit.rb:

class Badge
  def users
    sashes_with_badge = BadgesSash.where(badge_id: id).pluck(:sash_id)
    User.where(sash_id: sashes_with_badge)
  end
end

And then use the syntax you propose:

users_array = Badge.find(1).users

The reason is not easy to do in merit is that any other resource (not only users) may have reputation information.

@tute
Copy link
Member

tute commented Feb 2, 2013

I'll leave the issue open until I either document this in the wiki or README, or make it nicer (hook in has_merit model call to dynamically add the method is an option for instance).

Thanks for your feedback!

@Shpigford
Copy link
Author

Thanks so much!

@tute tute closed this as completed in e5b7594 Feb 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants