This project is archived and is in readonly mode.
Can't overwrite select for has_many through
Reported by Michał Łomnicki | November 30th, 2010 @ 12:25 PM
rails 3.0.3
ruby 1.9.2p53
Using association.select('...') for has_many through appends columns instead of overwriting them. With rails 2.3 it works as intended
class Group < ActiveRecord::Base
end
class MessageGroup < ActiveRecord::Base
belongs_to :message
belongs_to :group
end
class Message < ActiveRecord::Base
has_many :message_groups
has_many :groups, :through => :message_groups
end
Message.find(8).groups.select('groups.id')
SELECT "groups".*, groups.id FROM "groups" INNER JOIN "message_groups"
ON "groups".id = "message_groups".group_id WHERE (("message_groups".message_id = 8))
Correct query should look like
SELECT "groups".id FROM "groups" INNER JOIN "message_groups" ON
"groups".id = "message_groups".group_id WHERE
(("message_groups".message_id = 8))
Using
Message.find(8).groups.all(:select => 'groups.id')
produces the same wrong resultComments and changes to this ticket
-
Michał Łomnicki December 12th, 2010 @ 12:50 AM
- Tag changed from activerecord rails3, has_many_through, select to activerecord rails3, has_many_through, patch, select
I've prepared patch and test case for this issue
-
Jon Leighton December 21st, 2010 @ 09:38 PM
- State changed from new to open
- Assigned user set to Aaron Patterson
- Importance changed from to Low
I can confirm this is an issue and that the proposed patch fixes it. However, it did not apply cleanly, so I've updated it and attached. I've also removed the modification to
:readonly
inconstruct_find_scope
as it's not clear what this change is for, and it lacks a test. Please open another issue for that change if necessary (perhaps look at #5442?) -
Repository December 23rd, 2010 @ 11:20 PM
- State changed from open to resolved
(from [030480ac1f4fbf8bf74a0d9298544426caf26894]) Fix behaviour of foo.has_many_through_association.select('custom select') [#6089 state:resolved] https://github.com/rails/rails/commit/030480ac1f4fbf8bf74a0d9298544...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 6089 Can't overwrite select for has_many through (from [030480ac1f4fbf8bf74a0d9298544426caf26894]) Fix beh...