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.

Infinite loop when using has_many :through with nonstandard keys

#3628

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

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

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

Activity

  1. 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:16 AM