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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:30:25+00:00 2026-05-11T21:30:25+00:00

I have 3 models: Books, Notifications, and NotificationTypes. Books have notifications as the Notification

  • 0

I have 3 models: Books, Notifications, and NotificationTypes. Books have notifications as the Notification model has a book_id. Notifications have one notification_type as the Notification model has one notification_type_id

I want all Books created between date1 and date2

books_set1 = Book.find :all, :conditions => ["created_at <= ? AND show_time >= ?", max_date, min_date]

But I do not want books that have notifications of notification.notification_type_id = 1 and ideally i would like to say this by referring to notification_type.name so i would not want books that have notifications of notification.notification_type.name = ‘type1’

If there has been a notification of type1 created for a book already, I do not want it returned in the set (because i am going to create notifications of that type with the returned set).

I am not sure if there is a way to do this in one query, I am thinking I need 2 queries with and INTERSECT – the first I already included in this post and the second i am not sure about. But in pseudo-code, i think this is what it needs to do:

notification_type_id = Notification.find_by_name('type1')
get all notifications where notification_id = notification_type_id
set2 = get the associated book set from the notification set (since each notification has one book)

then i do set1 – set2

UPDATE

Thanks to some help I have written two queries that get me the desired results. I would love for this to be in 1 query if anyone knows how to do it:

books_in_range = Book.find :all, :conditions => ["created_at <= ? AND created_at >= ?", max_date, min_date]
books_without_these_notifications = Book.find(:all, :joins => { :notifications => :notification_type }, :conditions => ["notification_types.name = ?","type1"] )
books_to_consider = books_in_range - books_without_these_reminders

Again, the idea is to get all books that do not have a notification of type1 created and fall within a specific date range.

  • 1 1 Answer
  • 1 View
  • 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-11T21:30:26+00:00Added an answer on May 11, 2026 at 9:30 pm

    You could do it with NOT EXISTS but I’m not sure how efficient it would be:

    Book.find(
      :all, 
      :conditions => 
        ['created_at <= ? AND show_time >= ? AND 
          NOT EXISTS 
            (SELECT * FROM notifications 
             INNER JOIN notification_types ON notifications.notification_type_id = notification_types.id 
             WHERE notification_types.name = ? AND notifications.book_id = books.id)',
         max_date, min_date, 'type1'
        ])
    

    You might be able to come at it from a different direction and keep track of those which have had a notification of type1 sent out by adding a boolean to books or some other object joined directly to books. Then your query could be:

    Book.find(:all, 
      :conditions => 
        ['created_at <= ? AND show_time >= ? AND 
          type1_notification_sent = ?', 
         max_date, min_date, false
        ])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models books and authors joined by books_authors model. A book has
Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
If i have two tables Books, CDs with corresponding models. I want to display
I have HABTM models Client and Book . Client model has a bookshelf_color attribute
I have two simple models: Book, and Author Each Book has one Author, linked
I have 03 models book->target_readers and book has collection. class Book(models.Model): def __unicode__(self): return
I have two models: Books and Chapters, where book has many chapters. I've set
I have models of books and people: from django.db import models class Book(models.Model): author
I have two models. Writers and Books. A writer has many books. After i
I have a books model with a date type column named publish_date. On my

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.