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

  • SEARCH
  • Home
  • 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 8319165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:14:10+00:00 2026-06-08T22:14:10+00:00

Maybe the title is confusing, but I didn’t know how to explain my doubt.

  • 0

Maybe the title is confusing, but I didn’t know how to explain my doubt.

Say I have the following class methods that will be helpful in order to do chainings to query a model called Player. A Player belongs_to a User, but if I want to fetch Players from a particular village or city, I have to fetch the User model.

def self.by_village(village)
  joins(:user).where(:village => "village")
end

def self.by_city(city)
  joins(:user).where(:city => "city")
end

Let’s say I want to fetch a Player by village but also by city, so I would do…

Player.by_city(city).by_village(village).

This would be doing a join of the User twice, and I don’t think that is correct.. Right?

So my question is: What would be the correct way of doing so?

  • 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-06-08T22:14:12+00:00Added an answer on June 8, 2026 at 10:14 pm

    I haven’t tried that, but I would judge the answer to your question by the actual sql query ActiveRecord generates. If it does only one join, I would use it as you did, if this results in two joins you could create a method by_village_and_city.

    OK. Tried it now:

    1.9.2p290 :022 > Player.by_city("Berlin").by_village("Kreuzberg")
      Player Load (0.3ms)  SELECT "players".* FROM "players" INNER JOIN "users" ON "users"."id" = "players"."user_id" WHERE "users"."city" = 'Berlin' AND "users"."village" = 'Kreuzberg'
     => [#<Player id: 1, user_id: 1, created_at: "2012-07-28 17:05:35", updated_at: "2012-07-28 17:05:35">, #<Player id: 2, user_id: 2, created_at: "2012-07-28 17:08:14", updated_at: "2012-07-28 17:08:14">] 
    

    So, ActiveRecors combines the two queries, does the right thing and I would use it, except:
    I had to change your implementation though:

    class Player < ActiveRecord::Base
      belongs_to :user
      def self.by_village(village)
        joins(:user).where('users.village' => village)
      end
    
      def self.by_city(city)
        joins(:user).where('users.city' => city)
      end
    end
    

    and what you’re doing is usually handled with parameterized scopes:

    class Player < ActiveRecord::Base
      belongs_to :user
    
      scope :by_village, lambda { |village| joins(:user).where('users.village = ?', village) }
      scope :by_city, lambda { |city| joins(:user).where('users.city = ?', city) }
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the title may be confusing (or even misleading), but I'm planning
Ok. I know the title may be confusing. Logic That I have implemented is
Maybe title is confusing you, but the problem is concretely here : I have
Maybe the title is confusing, but I will try to explain with examples: Given
The title may be a little confusing, but I don't know how else to
Maybe a confusing title, what I'm after is a class, which only has certain
Ok the title maybe a little confusing. I have a database with the table
Maybe my title cannot fully explain my intention. I have a list of data
Maybe the title is a little bit obscure, but let me explain... In a
I realise that the title may be very confusing, so to explain what I

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.