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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:17:16+00:00 2026-05-23T18:17:16+00:00

OK, I recently had great answers to a question about how to do some

  • 0

OK, I recently had great answers to a question about how to do some sub-selects in an activerecord query. I now have a more complicated situation that I can’t figure out.

I want to do a search across 3 tables that have a many-to-one relationship with a :through , eg

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

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

class Parasite << ActiveRecord::Base
  belongs_to :pets
end

Now let’s say I have some data like so

users

id        name
1         Bob
2         Joe
3         Brian

pets

id        user_id  animal
1         1        cat
2         1        dog
3         2        cat
4         3        dog

parasites

id        pet_id    bug
1         1         tick
2         2         flea
3         3         tick
4         3         flea
5         4         tick

What I want to do is create an active record query that will return a user that has a pet which has both ticks and fleas (i.e. User 2 – Joe)

This is so far beyond my activerecord and sql skills that I won’t even bother to show you my bungled attempts so far.

  • 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-23T18:17:17+00:00Added an answer on May 23, 2026 at 6:17 pm

    This is pretty much the same as the previous question, you only need to dig one level deeper on the sub-selects:

    User.where('id IN (SELECT user_id FROM pets WHERE
      id IN (SELECT pet_id FROM parasites WHERE bug = ?) AND
      id IN (SELECT pet_id FROM parasites WHERE bug = ?))', 'flea', 'tick')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had a similar thread about this, but now I need to animate
I have recently been doing some circle detection in OpenCV and I have had
I have recently had need to checkout an apache project to do some fact-finding/debugging
I recently had a discussion with a colleague about serialization of byte data over
I recently had an exchange with another C++ developer about the following use of
I recently asked a question (and had it answered) here: jQuery Load JSON I
Question If you have what you think is a great idea, how do you
I had this problem a long time ago and recently I got some clues
I most recently had great progress in getting Vertex buffer objects to work. So
I recently had to perform some string replacements in .net and found myself developing

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.