This project is archived and is in readonly mode.

#3628 ✓stale
aubrey

Infinite loop when using has_many :through with nonstandard keys

Reported by aubrey | December 28th, 2009 @ 09:35 PM

If you attempt the create a has_many :through association that uses a has_many association with nonstandard primary and foreign keys, you will get an infinite loop when trying to use the association.

This happens because the association calls owner_quoted_id in construct_quoted_owner_attributes, and owner_quoted_id calls quote_value. The problem is that quote_value is not defined on the association itself and needs to be called through method_missing. The method_missing implementation in association_collection tries to send the method through to the reflection's class, but only after calling construct_scope, and that method ends up calling owner_quoted_id again, creating an infinite loop. The attached patch against 2-3-stable implements the quote_value method on AssociationCollection to send the call directly to the reflection's class without going through method_missing.

Here is a code example:

class MailingList < ActiveRecord::Base
  has_many :subscriptions
end

class Subscription < ActiveRecord::Base
  belongs_to :mailing_list
  belongs_to :user, :primary_key => :email, :foreign_key => :email
end

class User < ActiveRecord::Base
  has_many :subscriptions, :primary_key => :email, :foreign_key => :email, :dependent => :destroy
  has_many :mailing_lists, :through => :subscriptions, :primary_key => :email, :foreign_key => :email
end

User.first.mailing_lists => # infinite loop

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar October 9th, 2010 @ 03:16 AM

    • 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.

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>

Attachments

Pages