#1505 confirmed

Querying via a HABTM relationship broken

Reported by mltsy | April 20th, 2011 @ 09:33 AM

Perhaps this syntax simply isn't supported - but if not, I would expect an error, not an incorrect result:

Using MySQL, and with a structure like this:

class Business
  has n, :categories, :through => Resource

class Category
  has n, :businesses, :through => Resource

I would expect the following to return all businesses in category 1:

Business.all(:categories => {:id => 1})

But instead, it returns all businesses with :id => 1. I don't know if this is specific to MySQL, or a general bug.

The first SQL statement it issues selects category.id from categories JOIN business_categories JOIN businesses where category.id = 1 (selecting category.id where category.id = 1 is kinda useless... right? It just returns '1' a few times)

Comments and changes to this ticket

  • mltsy

    mltsy April 20th, 2011 @ 10:16 AM

    I'm guessing the fix here is to make it select business.id rather than category.id - but I don't know enough about DataMapper to say anything more than that.

  • Piotr Solnica (solnic)

    Piotr Solnica (solnic) April 20th, 2011 @ 11:25 AM

    • State changed from “new” to “confirmed”

    Try this as a workaround:

    # or if you have many ids
    Category.all(:id => [1,2,3,4]).businesses
    # or dot notation
    Business.all('categories.id' => [1,2,3,4])
  • mltsy

    mltsy April 20th, 2011 @ 04:53 PM

    Wow - I did not even know about that 'quoted-string' dot notation!

    I have only seen and read about the :symbol notation, which wasn't working.

    This works like a charm! Thanks :)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket