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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:28:17+00:00 2026-05-21T21:28:17+00:00

Say I have 2 models, Album and Image , and a join model named

  • 0

Say I have 2 models, Album and Image, and a join model named AlbumImage (and 3 corresponding tables in the database).

The models share a has-and-belongs-to-many association – Album has many images through album_images, and vice versa.

Tl;dr version

How can I find every image which does not appear within a given album?

Long version

I’m looking for something equivalent to the following SQL:

SELECT * FROM images
WHERE NOT EXISTS (
    SELECT * FROM album_images
    WHERE album_images.image_id = images.id
    AND album_images.album_id = ?
);

i.e. select every image where there does not exist a row in the album_images table with the same image ID and the ID of a given album.

But I unfortunately have no idea how to express this in Rails’ query syntax.

  • 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-21T21:28:18+00:00Added an answer on May 21, 2026 at 9:28 pm

    Try this:

    Option 1

    Image.all(:conditions => ["images.id NOT IN ( 
      SELECT a.image_id FROM album_images a WHERE a.album_id = ?)", alb_id])
    

    This approach is better than using NOT EXISTS as sub query result here is cached by DB.

    Option 2

    Using LEFT OUTER JOIN.

    Image.all(:joins => "LEFT OUTER JOIN album_images a 
        ON a.album_id = #{alb_id} AND a.image_id = images.id",
     :conditions => "a.image_id IS NULL")
    

    Option 3

    If the number of images per album is limited and you dont mind incurring the cost of one extra query:

    conditions = ["id NOT IN (?)", 
      @album.album_images.map(&:image_id)] unless @album.album_images.empty?
    
    Image.all(:conditions => conditions)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
Let's say i have many models like that: class ExampleModel(models.Model): photo = models.ImageField(upload_to='photos/') image
Say I have the following in my models.py : class Company(models.Model): name = ...
Let's say I have the following model: class Contest: title = models.CharField( max_length =
Say I have two models in Django - an Actor and a Movie model
say I have these models: class Parent(models.Model): slug = models.SlugField() class Child(models.Model): slug =
Lets say I have three models/tables: operating_systems , words , and programming_languages : #
Say I have the following models: class Baz(models.Model): winning = models.CharField(max_length=100) class Bar(models.Model): baz
Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)

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.