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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:45:38+00:00 2026-05-31T08:45:38+00:00

Hi there, I have a little problem with a where query on a has_many

  • 0

Hi there,

I have a little problem with a where query on a has_many :through association…

My setup is as follows:

PurchaseOrderAddressAssignment:

belongs_to :address

belongs_to :purchase_order

Address:

has_many :purchase_order_address_assignments

has_many :purchase_orders, :through => :purchase_order_address_assignments

PurchaseOrder:

has_many :purchase_order_address_assignments

has_many :addresses, :through => :purchase_order_address_assignments

My where clause:

PurchaseOrder.where("addresses.id = 168 and addresses.id = 169").includes(:addresses)

Is returning 0 Records… but there should be at least 1…

PurchaseOrder.where(:baan_id => "KD0005756").first.address_ids

Is returning [168, 169, 170, 327]

… I think I’m too stupid to solve this little problem :-/

Can someone tell me what I’m doing wrong here?

Thx,

Michael

  • 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-31T08:45:40+00:00Added an answer on May 31, 2026 at 8:45 am

    I would probably do a custom finder method in this case.

    class PurchaseOrder < ActiveRecordBase
      def self.with_addresses(*args)
        values = args.flatten.uniq
    
        # Note use :joins instead of :includes if you don't
        # want the addresses data
        includes(:addresses)
        where(:addresses => {:id => values})
        group("purchase_orders.id")
        having("count(ad‌​dresses.id)=#{values.size}")
      end
    end
    

    I’m pretty sure that should work.

    Here’s a similar answer that helps explain the query.

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

Sidebar

Related Questions

My problem is a little bit hard but simple. I have (or there is)
I have a little problem in object programming in javascript There is a class
I have a little problem. I want to do automatic searching, but there is
On this page there is a list of footer links that have a little
This is a little out there but I have a customer object coming back
I have been working on a childish little program: there are a bunch of
I have a little problem that I don't understand. I have a db that
I have a little problem with my apache2 and .htaccess rules. for example: I
I have a little, but nasty problem: I have a database with many records,
I'm a little new to SQL and have come across the following problem. I

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.