This project is archived and is in readonly mode.

#2127 ✓committed
Jan De Poorter

Broken find last when no order is given.

Reported by Jan De Poorter | March 4th, 2009 @ 03:43 PM

When finding the last item through an association or a named_scope in an association I get the error:


User.first.logs.find_last_by_type('foo')
NoMethodError: private method `split' called for nil:NilClass
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:1540:in `reverse_sql_order'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:1529:in `find_last'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:614:in `find'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:1907:in `find_last_by_credits'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:1892:in `send'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:1892:in `method_missing'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:374:in `send'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:374:in `method_missing'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/base.rb:2148:in `with_scope'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:206:in `send'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:206:in `with_scope'
	from ./../frontend/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:370:in `method_missing'
	from (irb):5

I tracked it down to scoped? returning true because the :order key was set by the association, but was nil. Attached is a patch that fixes that by checking if the key is there and is not nil

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>

People watching this ticket

Attachments

Referenced by

Pages