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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:20:38+00:00 2026-05-16T17:20:38+00:00

I have an ActiveRecord called Name which contains names in various Languages . class

  • 0

I have an ActiveRecord called Name which contains names in various Languages.

class Name < ActiveRecord::Base
  belongs_to :language

class Language < ActiveRecord::Base
  has_many :names

Finding names in one language is easy enough:

Language.find(1).names.find(whatever)

But I need to find matching pairs where both language 1 and language 2 have the same name. In SQL, this calls for a simple self-join:

SELECT n1.id,n2.id FROM names AS n1, names AS n2
  WHERE n1.language_id=1 AND n2.language_id=2
    AND n1.normalized=n2.normalized AND n1.id != n2.id;

How can I do a query like this with ActiveRecord? Note that I need to find pairs of names (= both sides of the match), not just a list of names in language 1 that happens to match with something.

For bonus points, replace n1.normalized=n2.normalized with n1.normalized LIKE n2.normalized, since the field may contain SQL wildcards.

I’m also open to ideas about modeling the data differently, but I’d prefer to avoid having separate tables for each language if I can.

  • 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-16T17:20:39+00:00Added an answer on May 16, 2026 at 5:20 pm

    Try this:

    ids = [1,2]
    Name.all(:select    => "names.id, n2.id AS id2",
             :joins     => "JOIN names AS n2 
                                  ON n2.normalized = names.normalized AND 
                                     n2.language_id != names.language_id AND
                                     n2.language_id IN (%s)" % ids.join(','),
             :conditions => ["names.language_id IN (?)", ids]
    ).each do |name|
      p "id1 : #{name.id}"
      p "id2 : #{name.id2}"
    end
    

    PS: Make sure you sanitize the parameters passed to the join condition.

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

Sidebar

Related Questions

I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a table called Users ( class User < ActiveRecord::Base ) and a
I have a basic has_many through relationship: class Foo < ActiveRecord::Base has_many :bars, :dependent
I have a Family class so defined: class Family < ActiveRecord::Base after_initialize :initialize_family belongs_to
I have class Car < ActiveRecord::Base; end class Porsche < Car has_many :page_items, :as=>:itemable,
I have an ActiveRecord model with a field called name (in the database) and
I have an ActiveRecord before_save method throwing an odd error: class MyThing < ActiveRecord::Base
I have a standard ActiveRecord model with the following: class MyModel < ActiveRecord::Base custom_method
The following are the models and association. class Vendor < ActiveRecord::Base attr_accessible :name, :address_attributes
Here's my User model: class User < ActiveRecord::Base has_many :friends, :class_name => 'Friendship', :dependent

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.