Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 761509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:50:47+00:00 2026-05-14T15:50:47+00:00

I have three models: User Award Trophy The associations are: User has many awards

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:50:47+00:00Added an answer on May 14, 2026 at 3:50 pm

    I am always comfortable with the “find_by_sql” You can use it
    Using find_by_sql as follows

    User.find_by_sql("select u.id, u.name, t.trophy_type 
                        from users u, awards a, trophies t
                        where a.user_id=u.id and 
                        t.trophy_id=a.id and 
                        t.trophy_type = 'GoldTrophy'"
                     )
    

    I am not sure using “named_scope” But try this

    class User < ActiveRecord::Base
    
        named_scope :gold_trophy_holder, 
                    :select=>" users.id, users.name, trophies.trophy_type",       
                    :joins => :awards, "LEFT JOIN awards ON awards.id = trophies.award_id"
                    :conditions => ['trophies.trophy_type = ?', 'GoldTrophy']
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: User, Task and Assignation. User has many tasks through assignation.
I have three models: user, subscription, and channel. User: has_many :channels, :through => :subscriptions
I have three models of concern here: User Fight FightPunches Punches The associations are
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have three models: Post , Comment , User and Vote . I'm using
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
According to an example , I have three models: class User(models.Model): name = models.CharField(max_length=128)
Currently I have three models in Django that create a circular reference: A User
I have three models ( User , Tag , Product ) and they interact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.