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.

Can't overwrite select for has_many through

#6089

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

Reported by Michał Łomnicki · November 30th, 2010 @ 12:25 PM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Michał Łomnicki
    Michał Łomnicki
    • 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

    December 12th, 2010 @ 12:50 AM

  2. Jon Leighton
    Jon Leighton
    • 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 in construct_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 Model.has_many_through_association.find(id) returns a read-only record?)

    December 21st, 2010 @ 09:38 PM

  3. Repository
    Repository
    • State changed from open to resolved

    December 23rd, 2010 @ 11:20 PM