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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:30:18+00:00 2026-05-23T10:30:18+00:00

Im working on Rails 3.0.5 and PostgreSQL. I have a model Offer , that

  • 0

Im working on Rails 3.0.5 and PostgreSQL.

I have a model Offer, that has many Products.

class Offer < ActiveRecord::Base
  has_many  :products
end

class Product < ActiveRecord::Base
   belongs_to :offer
end

The product has an id that updates when you register it in a third party service. Lets call it service_id. I want an Offer scope, that gets all the offers that have every single product registered in the third party service. In other words, I’m only interested in the offer instance if all of its products have a service id.

The offer scope: joins(:products).where("products.service_id is not NULL"), just returns the offers that have at least one product that is not null.

Any help would be appreciated.

(PS: Sorry for the title, I tried to explain it the best I could.)

EDIT:

For example: If I have 2 offers: Offer1 and Offer2. Offer1 has products p1 and p2, and Offer2 has products p3 and p4. Let’s say p1, p2 and p3 have a service but p4 doesn’t. The query should return only Offer1.

  • 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-23T10:30:18+00:00Added an answer on May 23, 2026 at 10:30 am

    This is another approach.

    Offer.find_by_sql("SELECT * FROM offers o WHERE NOT EXISTS (SELECT * FROM products WHERE products.offer_id = o.id AND service_id IS NULL)")
    

    Even do the idea of an ORM is that you abstract the SQL, in this kind of complex query, I think it is better to pick the simplest solution and not complicate the query even more.

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

Sidebar

Related Questions

I am thinking of working on a Rails application that uses PostgreSQL. I have
I have a has_many :through relationship set up like so class Situation < ActiveRecord::Base
I'm working on a rails app that has a whole bunch of before filters
I have a working Rails application on version 2.3.5 - I am using many
I have a working rails 3.1 application that uses the mercury wysiwyg editor, however
I am working with Rails 2.3 and I have the following associations: User: has_many
I working in Rails 3 Activerecord. I have two models City and Shipment and
I working on Ruby on Rails application using a PostgreSQL database. I have heard
I'm working on a Ruby on Rails/PostgreSQL web app that a mobile device communicates
Now that I have a working Rails 3 web app, I'd like to make

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.