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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:09:37+00:00 2026-05-24T02:09:37+00:00

Okay, I feel like I’m starting to come to SO for every activerecord query

  • 0

Okay, I feel like I’m starting to come to SO for every activerecord query I have to write now and I’m starting to drag out my user/pet/parasite metaphor but here we go again.

In the following setup;

class User < ActiveRecord::Base
  has_many :pets
  has_many :parasites, :through => :pets
end

class Pet < ActiveRecord::Base
  has_many :parasites
  belongs_to :user
end

class Parasite < ActiveRecord::Base
  belongs_to :pet
end

I want to write a search that will return all of the parasites that belong to Bob’s cat (i.e. User.name = ‘Bob’ and Pet.animal = ‘Cat’).

I realise I can do this with the fairly drawn out and ugly

User.where(:name => 'Bob').first.pets.where(:animal => 'Cat').first.parasites

but I thought there should be a more succinct way of doing this.

All of my attempts to write a join statement to make this happen result in an ActiveRecord::Configuration error so I suspect I am going about this backwards. Once again, this seems like it should be easier than it is.

Thanks.

  • 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-24T02:09:39+00:00Added an answer on May 24, 2026 at 2:09 am

    You try to achieve a has_many through has_many association. This won’t work using Rail’s eager loading associations.

    What you have to do is:

    • join the users
    • join the pets
    • scope users down by username
    • scope pets down by user_id and the animal field

    In ActiveRecord:

    Parasite.joins(:pet).joins('INNER JOIN users').where('users.name = ? AND pets.user_id = users.id AND pets.animal = ?', @username, @animal)
    

    Alternatively you can create a named scope:

    class Parasite < ActiveRecord::Base
      belongs_to :pet
      scope :parasites_of, lambda {|owner, animal_type| joins(:pet).joins('INNER JOIN users').where('users.name = ? AND pets.user_id = users.id AND pets.animal = ?', owner, animal_type) }
    end
    

    Now you can call Parasite.parasites_of('Bob', 'Cat')

    The resulting SQL Query will look like:

    SELECT * FROM parasites
     INNER JOIN users,
     INNER JOIN pets ON pets.id = parasites.pet_id
    WHERE
      users.name = 'Bob'
      AND
      pets.user_id = users.id
      AND
      pets.animal = 'Cat'
    

    (Hint: The .to_sql method will show you the plain SQL query)

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

Sidebar

Related Questions

Okay, I feel like I should be able to do this, but I have
Okay I almost have this rebase thing figured out. I can feel a breakthrough
Okay, I feel like I am missing something really simple here. I have an
Okay, I feel like a bit of a 800 pound gorilla trying to thread
Okay, if feel like this should be really simple and accomplished by a function
Feel like this should be something easy that I'm missing. I have a table
Okay so, I have read that .INI files have become obsolete now and the
Okay, I feel totally stupid posting this. I am not sure if it is
Okay so say I have a fixed width TextView and the text inside it
Okay, what have I done wrong here? I'm trying to .POST data from a

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.