Weird bug in association extensions


#1

Hey all, just upgraded an older app to latest rspec and rails. This
code runs fine in console but fails under rspec.

class User
  has_many :statuses, :extend => Status:::AssociationExtension
end

module Status::AssociationExtensions
  def after(status)
    find(:first, :conditions => ['statuses.created_at > ?',

status.created_at], :order => ‘statuses.created_at’)
end
end

This code works in the app and from console

recent = @user.statuses.first
status = @user.statuses.after(recent)

But in the spec, I get this: Weird, eh? It thinks that “self.class” ==
Array and can’t find the “find” method on the array instance. Is this
something in my code somewhere, or did something change with
association collections?

NoMethodError in ‘StatusesController PUT #update StatusesController
(unsuccessful save) assigns @status
undefined method find' for #<Class:0x2521c94> /Users/courtenay/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/base.rb:1964:inmethod_missing_without_paginate’
/Users/courtenay/dev/entp/tt/vendor/plugins/will_paginate/lib/
will_paginate/finder.rb:164:in method_missing' /Users/courtenay/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/associations/association_collection.rb:60:infind’
/Users/courtenay/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/
active_record/associations/association_collection.rb:395:in
find_target' /Users/courtenay/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/ active_record/associations/association_collection.rb:349:inload_target’
/Users/courtenay/.gem/ruby/1.8/gems/activerecord-2.3.2/lib/
active_record/associations/association_proxy.rb:139:in `inspect’
spec/controllers/statuses_controller_spec.rb:249:
spec/controllers/statuses_controller_spec.rb:282:


#2

On Sat, May 9, 2009 at 2:01 AM, court3nay removed_email_address@domain.invalid wrote:

status.created_at], :order => ‘statuses.created_at’)
something in my code somewhere, or did something change with
association collections?

The only direct handling of AssociationProxy in rspec-rails is if
you’re using the change matcher. Does the failing example have ‘should
change’ in it? Could be an rspec bug in that case. If not, I’m not
sure what else it could be. Can you try converting it to a test/unit
test_case and see if you get the same result?


#3

court3nay wrote:

status.created_at], :order => ‘statuses.created_at’)
something in my code somewhere, or did something change with
association collections?

Personally I just always run the debugger whenever I encounter some
weird fucked up thing like this in rails. Better to know that to guess.

But, if I recall correctly, the association proxy is just an Array which
includes a ton of modules - some of which come from AR.

http://gist.github.com/10c44efda79f31fd2443

Scott