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 3989426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:22:19+00:00 2026-05-20T06:22:19+00:00

We have a Customer model, which has a lot of has_many relations, e.g. to

  • 0

We have a Customer model, which has a lot of has_many relations, e.g. to CustomerCountry and CustomerSetting. Often, we need to join these relations to each other; e.g. to find the settings of customers in a given country. The normal way of expressing this would be something like

CustomerSetting.find :all,
                     :joins => {:customer => :customer_country},
                     :conditions => ['customer_countries.code = ?', 'us']

but the equivalent SQL ends up as

SELECT ... FROM customer_settings 
INNER JOIN customers ON customer_settings.customer_id = customers.id
INNER JOIN customer_countries ON customers.id = customer_countries.customer_id

when what I really want is

SELECT ... FROM customer_settings
INNER JOIN countries ON customer_settings.customer_id = customer_countries.customer_id

I can do this by explicitly setting the :joins SQL, but is there an idiomatic way to specify this join?

  • 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-20T06:22:20+00:00Added an answer on May 20, 2026 at 6:22 am

    Besides of finding it a bit difficult wrapping my head around the notion that you have a “country” which belongs to exactly one customer:

    Why don’t you just add another association in your model, so that each setting has_many customer_countries. That way you can go

    CustomerSetting.find(:all, :joins => :customer_countries, :conditions => ...)
    

    If, for example, you have a 1-1 relationship between a customer and her settings, you could also select through the customers:

    class Customer
      has_one :customer_setting
      named_scope :by_country, lambda { |country| ... }
      named_scope :with_setting, :include => :custome_setting
      ...
    end
    

    and then

    Customer.by_country('us').with_setting.each do |cust|
      setting = cust.customer_setting
      ...
    end
    

    In general, I find it much more elegant to use named scopes, not to speak of that scopes will become the default method for finding, and the current #find API will be deprecated with futures versions of Rails.

    Also, don’t worry too much about the performance of your queries. Only fix the things that you actually see perform badly. (If you really have a critical query in a high-load application, you’ll probably end up with #find_by_sql. But if it doesn’t matter, don’t optimize it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a business model called Customer which has many required properties (via DataAnnotations)
I have a Customer model where each customer has many jobs: class Customer <
I have an Invoice model which belongs to Customer (and of course Customer has_many
I have a Customer model which contains a ForeignKey to a Contact model. I
I'm using active admin for my rails app. I have a customer model which
Once again, I have my form_for for my Order model, which has_many :services, :through
I have a item tracking system which has an Item model which has a
I have a data model as follows: A Customer has Products and Payment Methods.
I have a form changeed the properties of my object CUSTOMER. Each customer has
I have a model called Customer in google app engine with python: class Customer(db.Model):

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.