This project is archived and is in readonly mode.

#6089 ✓resolved
Michał Łomnicki

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 result

Comments and changes to this ticket

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>

Referenced by

Pages