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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

Invoking a named_scope with :conditions => ["id in (?)", association_proxy]

#2634

Invoking a named_scope with :conditions => ["id in (?)", association_proxy] hits infinite recursion.


class Person < ActiveRecord::Base

  has_many :household_people
  has_many :households, :through => :household_people
  
  named_scope :in, lambda {|people|
    {
      :conditions => ["id in (?)", people]
    }
  }

  named_scope :foo, :conditions => "1 = 1"
end

class HouseholdPerson < ActiveRecord::Base
  belongs_to :household
  belongs_to :person
end

class Household < ActiveRecord::Base
  has_many :household_people
  has_many :people, :through => :household_people
end

>> Person.create! :name => "Joe"
>> Person.create! :name => "Pete"
>> Person.create! :name => "Andy"

>> Household.create! :name => "Brady"
>> Household.create! :name => "Cosby"

>> Person.first.households << Household.first
>> Person.last.households << Household.first

>> Person.in(Person.foo.map(&:id)) # => works
>> Person.in(Person.all) # => works

>> Person.in(Person.foo) # => hangs forever

>> Person.in(Household.first.people) # => infinite recursion

SystemStackError: stack level too deep
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:128:in `construct_conditions'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:75:in `find_target'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:349:in `load_target'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_proxy.rb:212:in `method_missing'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:366:in `method_missing'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2339:in `sanitize_sql_array'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2235:in `sanitize_sql'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1499:in `merge_conditions'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1497:in `each'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1497:in `merge_conditions'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1809:in `add_conditions!'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1692:in `construct_finder_sql'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1553:in `find_every'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:615:in `find'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/has_many_through_association.rb:73:in `find_target'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/association_collection.rb:349:in `load_target'
... 4251 levels...
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in `with_scope'
	from (__DELEGATION__):2:in `__send__'
	from (__DELEGATION__):2:in `with_scope'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:179:in `method_missing'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:193:in `load_found'
	from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:171:in `proxy_found'
	from (__DELEGATION__):2:in `inspect'
	from /Library/Ruby/Gems/1.8/gems/wirble-0.1.2/./wirble.rb:417:in `output_value'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:151:in `eval_input'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:259:in `signal_status'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:147:in `eval_input'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:70:in `start'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `catch'
	from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/irb.rb:69:in `start'

App attached.

Reported by Ben Woosley · May 11th, 2009 @ 05:16 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

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

    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 @ 04:16 AM

  3. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:57 AM