named_scope or find_by_sql?

Posted by keruilin on Stack Overflow See other posts from Stack Overflow or by keruilin
Published on 2010-05-02T04:56:23Z Indexed on 2010/05/02 5:07 UTC
Read the original article Hit count: 311

I have three models:

  • User
  • Award
  • Trophy

The associations are:

  • User has many awards
  • Trophy has many awards
  • Award belongs to user
  • Award belongs to trophy
  • User has many trophies through awards

Therefore, user_id is a fk in awards, and trophy_id is a fk in awards.

In the Trophy model, which is an STI model, there's a trophy_type column. I want to return a list of users who have been awarded a specific trophy -- (trophy_type = 'GoldTrophy'). Users can be awarded the same trophy more than once. (I don't want distinct results.)

Can I use a named_scope? How about chaining them? Or do I need to use find_by_sql? Either way, how would I code it?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about named-scope