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

  • Home
  • SEARCH
  • 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 7572213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:52:09+00:00 2026-05-30T15:52:09+00:00

I have a has_many :through relationship set up like so class Situation < ActiveRecord::Base

  • 0

I have a has_many :through relationship set up like so

class Situation < ActiveRecord::Base
  has_many :notifications
  has_many :notiftypes, through: :notifications
end

class Notification < ActiveRecord::Base
  belongs_to :situation
  belongs_to :notiftype
end

class Notiftype < ActiveRecord::Base
  has_many :notifications
  has_many :situations, through: :notifications
end

So, a Situation has many Notifications, which can be of many types (Notiftype).

My problem is trying to query for the notiftypes that have not been set for a particular situation.

Want to find records with no associated records in Rails 3

The answers in that question get me close, but only to the point of finding Notiftypes that have not been set AT ALL.

If this were the standard :situation has_many :notiftypes I could just do a Left Outer Join like so

myquery = Notiftype.joins('LEFT OUTER JOIN situations ON situations.notiftype_id = notiftype.id').where('notiftype_id IS NULL')

but I’m really not sure how to do this with the intermediate table between them.

I have been trying consecutive joins but it’s not working. I’m not sure how to join the two separated tables.

Can anyone explain the right way to query the db? I am using SQLite, Rails 3.1, Ruby 1.9.2 right now, but likely Postgresql in the future.

  • 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-30T15:52:11+00:00Added an answer on May 30, 2026 at 3:52 pm

    Try this:

    class Situation < ActiveRecord::Base
      # ...
      has_many :notiftypes, through: :notifications do
    
        def missing(reload=false)
          @missing_notiftypes = nil if reload
          @missing_notiftypes ||= proxy_owner.notiftype_ids.empty? ?
            Notiftype.all : 
            Notiftype.where("id NOT IN (?)", proxy_owner.notiftype_ids)
        end
      end
    end
    

    Now to get the missing Notiftype

    situation.notiftypes.missing
    

    If you want to further optimize this to use one SQL rather than two you can do the following:

    class Situation < ActiveRecord::Base
      # ...
      has_many :notiftypes, through: :notifications do
    
        def missing(reload=false)
          @missing_notiftypes = nil if reload
          @missing_notiftypes ||= Notiftype.joins("
              LEFT OUTER JOIN (#{proxy_owner.notiftypes.to_sql}) A
              ON A.id = notiftypes.id").
            where("A.id IS NULL")
        end
      end
    end
    

    You can access the missing Notifytypes as:

    situation.notiftypes.missing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I have a basic has_many through relationship: class Foo < ActiveRecord::Base has_many :bars, :dependent
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
i have three models, all for a has_many :through relationship. They look like this:
I have the following models: require 'books_projects.rb' class Project < ActiveRecord::Base has_many :book_to_projects has_many
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events
I have a many-to-many relationship set up through a join model. Essentially, I allow

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.