Forum: RSpec Weird bug in association extensions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F7d94c4c159ec28690c0190bbd304ec4?d=identicon&s=25 court3nay (Guest)
on 2009-05-09 11:12
(Received via mailing list)
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?

6)
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:in `method_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:in `find'
/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:in
`load_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:
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-09 18:25
(Received via mailing list)
On Sat, May 9, 2009 at 2:01 AM, court3nay <court3nay@gmail.com> 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?
369b9fd2a0d9e7cdbc60907a2a056ad9?d=identicon&s=25 Scott Taylor (Guest)
on 2009-05-09 18:58
(Received via mailing list)
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
This topic is locked and can not be replied to.