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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:21:56+00:00 2026-05-15T13:21:56+00:00

I use example with Rails 3, but I believe that this is true for

  • 0

I use example with Rails 3, but I believe that this is true for Rails 2.3 as well.

Suppose, I have model City that has many Locations. I try to find Cities that have locations.

I use following code:

City.joins(:locations)

But output array is:

=> [#<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">]

Array length is 4 (number of locations of Moscow).

In what case it can be useful? For what aims are 4 copies of one object in output-array?

I can use City.joins(:locations).uniq, but I lost agile of arel.

I have two questions:

  1. Why joins returns non unique array?
  2. What is prefer to use instead of joins for this purpose?
  • 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-15T13:21:57+00:00Added an answer on May 15, 2026 at 1:21 pm

    Join essentially says to combine two tables and treat it like one table, sending back whatever would be found. That means it will find you every combination of city and location (Rails is helping out by matching things up based on the belongs_to/has_many relationship in your models).

    Doing City.joins(:locations) would be for finding all the locations in a city. Doing it the other way (Location.joins(:city)) would be for finding what city a location is in.

    Now, to just find the list of cities that have some locations, you might try City.select(:city).joins(:locations).group('cities.id') The select() clause tells it to just bring back the City, and the group() clause tells it to not bring back duplicate copies.

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

Sidebar

Related Questions

We have a model association that looks something like this: class Example < ActiveRecord::Base
To use as an example: lets say that I have 2 EditText s and
I would like to use a web server based on Rails. But I have
I use Rails 3 and RSpec 2.6.0. Not sure if that's possible, but here
I never seemed to have this problem while doing Rails work in Linux, but
I could really use an example of this. Can anyone provide me with a
Best to use an example to describe the problem. Lets say I have a
For more information see this Example use strict; use warnings; use CGI::Simple; use DBI;
In wiki article for REST it is indicated that if you use http://example.com/resources DELETE,
I am new to Rails so this is probably an easy question but can't

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.