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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:00:50+00:00 2026-06-05T03:00:50+00:00

I’m trying to do a multi-table join that has a NOT IN component. Tables

  • 0

I’m trying to do a multi-table join that has a NOT IN component. Tables are

Post -> Term Relationship -> Term

Post
  has_many :term_relationships
  has_many :terms, :through => :term_relationships

TermRelationship
  belongs_to :post
  belongs_to :term

Term
  has_many :term_relationships
  has_many :posts, :through => :term_relationships

The goal is to get all posts except for those in “featured” let’s say. My current query would looks like:

WpPost.includes(:terms).where("terms.term NOT IN (?)", ["featured"])

This works great if the only term that it has attached is “featured”. If the post belongs to “featured” and “awesome” it will still show because of “awesome”.

Anyway to exclude a row entirely? Will it require a subquery? And if it does, how would I go about doing that in rails?

Thanks all!
Justin

  • 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-06-05T03:00:51+00:00Added an answer on June 5, 2026 at 3:00 am

    You misuse the includes. It’s for eager loading, not for joining!

    But you’re right about the approach. It can be used in your case. But Rails won’t issue nested request for NOT IN (?) even if it would be logical. You’ll get 2 queries instead (you’ll get NOT IN (id1, id2....,) instead of NOT IN (SELECT ....)).

    So I would recommend you to use the squeel gem:

    regular AR code (can also be prettified with squeel):

    featured_posts = WpPost.joins(:terms).where(terms:{term: ['featured']}).uniq
    

    and then use the sqeel’s power:

    WpPost.where{id.not_in featured_posts}
    

    (in and not_in are also aliased as >> and << but I didn’t want to scary anybody)

    Note the using blocks and absence of symbols.


    Some measurements based on Chinook Database under SQLite:

    > Track.all
      Track Load (35.0ms)  SELECT "Track".* FROM "Track"
    

    Relation with joins and like:

    oldie = Track.joins{playlists}.where{playlists.name.like_any %w[%classic% %90%]}
    

    Here’s NOT IN:

    > Track.where{trackId.not_in oldie}.all
      Track Load (37.5ms)  SELECT "Track".* FROM "Track" WHERE "Track"."trackId" 
      NOT IN (SELECT "Track"."TrackId" FROM "Track" INNER JOIN "PlaylistTrack" ON
        "PlaylistTrack"."TrackId" = "Track"."TrackId" INNER JOIN "Playlist" ON 
        "Playlist"."PlaylistId" = "PlaylistTrack"."PlaylistId"
         WHERE (("Playlist"."name" LIKE '%classic%' OR "Playlist"."name" LIKE '%90%')))
    

    FYI:

    Track.where{trackId.not_in oldie}.count # => 1971
    Track.count # => 3503
    # join table:
    PlaylistTrack.count # => 8715
    

    Conclusion: I don’t see the overhead caused by NOT IN. 35.0 vs 37.5 isn’t noticeable difference. Few times 35.0 became 37.5 and vice verse.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.