This project is archived and is in readonly mode.

#2634 ✓stale
Ben Woosley

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

Reported by Ben Woosley | May 11th, 2009 @ 05:16 PM | in 3.x

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.

Comments and changes to this ticket

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