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.

Primary key value is not properly quoted with has_many :through using belongs_to

#3587

The has_many :through association where the through association is a belongs_to, (a feature introduced by #323 has_many :through => belongs_to_association bug) can generate SQL queries with an unquoted primary key value.

The following example will fail when the has_many :through association is used and Ocean's primary key value is something that needs quoting.

    class Fisherman < ActiveRecord::Base
      belongs_to :ocean
      has_many :crabs, :through => :ocean
    end

    class Ocean < ActiveRecord::Base
      # Ocean's primary key is e.g. a string
      has_many :crabs
    end
 
    class Crab < ActiveRecord::Base; end

Here is an example of what this might yield:

>> f = Fisherman.first
=> #<Fisherman id: 2, ocean_id: "atlantic">
>> f.crabs

ActiveRecord::StatementInvalid: [...]
 no such column: atlantic:
  SELECT "crabs".* FROM "crabs"
  INNER JOIN "oceans" ON "crabs".ocean_id = "oceans".id
  WHERE (("oceans".id = atlantic))

The bug is in HasManyThroughAssociation's construct_quoted_owner_attributes. It properly quotes the primary key value for the two other types of has_many :through, but not the belongs_to:

    # Associate attributes pointing to owner, quoted.
    def construct_quoted_owner_attributes(reflection)
      if as = reflection.options[:as]
        { "#{as}_id" => owner_quoted_id,
          "#{as}_type" => reflection.klass.quote_value(
            @owner.class.base_class.name.to_s,
            reflection.klass.columns_hash["#{as}_type"]) }
      elsif reflection.macro == :belongs_to
        { reflection.klass.primary_key => @owner[reflection.primary_key_name] }
      else
        { reflection.primary_key_name => owner_quoted_id }
      end
    end

This is clearly wrong as the method name and comment indicates that it should quote the attributes. The code in question was commited in 95e1cf4812.... I have attached a simple patch to add the needed quoting.

Reported by Gustav Broberg · December 16th, 2009 @ 09:07 PM

State: stale
Milestone: none
Assigned to: Pratik Pratik
Importance: Low

Activity

  1. Gustav Broberg
    Gustav Broberg
    • Assigned user set to Pratik

    December 17th, 2009 @ 01:30 AM

  2. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to Low

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 9th, 2010 @ 03:11 AM

  3. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:04 PM