Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] has_many :through _ids collection_reader methods should honor :uniq option

#6202

Hello there,

I've found what I believe to be a bug in the 2-3-stable branch. Given a model that contains a has_many :through association that uses the :uniq option like so:

class User < ActiveRecord::Base
  has_many :user_events
  has_many :events, :through => :user_events, :uniq => true
end

class UserEvent < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
end

class Event < ActiveRecord::Base
  has_many :user_events
  has_many :users, :through => :user_events
end

When I make a call for the elemnts of the unique association, the query generated uses a SELECT DISTINCT query like expected:

ree-1.8.7-2010.02 > u = User.first
ree-1.8.7-2010.02 > u.events
  Event Load (0.3ms)   SELECT DISTINCT "events".* FROM "events" INNER JOIN "user_events" ON "events".id = "user_events".event_id WHERE (("user_events".user_id = 1)) 
 => [#<Event id: 1, user_event_id: nil>]

However, when I make the call on the association's _ids method, it does not generate a DISTINCT query.

ree-1.8.7-2010.02 > u.event_ids
  Event Load (0.2ms)   SELECT "events".id FROM "events" INNER JOIN "user_events" ON "events".id = "user_events".event_id WHERE (("user_events".user_id = 1))

The expected behavior does seem to be working properly in Rails 3, attached is a patch that backports this behavior to the rails 2-3-stable branch.

Thanks!

Blake

Reported by Blake Smith · December 21st, 2010 @ 03:46 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Quigebo
    Quigebo

    +1

    Thanks for fixing this! Bake it away, toys.

    January 6th, 2011 @ 03:59 PM

  2. Ryan Kinderman
    Ryan Kinderman

    +1

    This is an annoying bug that we've had to patch our app for, and it'd be great to get it directly from the repo and delete our patch. Pretty straight-forward fix as well, so can't be that much risk or effort in patching it in.

    January 6th, 2011 @ 03:59 PM

  3. Keith Norman
    Keith Norman

    +1 But, why not just upgrade to Rails 3? ;)

    January 6th, 2011 @ 04:05 PM

  4. Joey Schoblaska
  5. Adam Walters
  6. anthony_caliendo
    anthony_caliendo

    +1 Simple fix, seems to work

    January 6th, 2011 @ 04:57 PM

  7. mloseke
  8. Alex Rakoczy
  9. Dave Nawara
  10. Dave Hoover
  11. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 8th, 2011 @ 01:00 AM

  12. rails
    rails
    • State changed from open to stale

    April 8th, 2011 @ 01:00 AM