I'm a Rails Contributor!

Posted by patjoyce
Nov 21, 2007

My first patch was committed to the Rails trunk yesterday. I’m pretty excited. The community on the rails-contrib IRC channel (particularly Rick Olson) were very helpful. Hopefully I will be able to contribute more in the future.

Patch 8174 fixes a bug where calling a dynamic finder on a has_many association ignored the :order of the association.

Background:

ActiveRecord Collections (has_many, has_many :through, and has_and_belongs_to_many) allow the use of find and dynamic finders just on the members of that collection.

Problem:

The find method behaves as expected, returning objects in the order specified in the :order parameter of the has_many association.

However, dynamic finders (find_by_xxx and find_all_by_xxx) do not respect the :order parameter passed to the has_many association.

Example:

A post has_many :comments, :order => ‘id desc’

my_post.comments.find(:first, :conditions => “comments.author_id = 1”) will return the comment with author_id 1 with the highest id.

However, my_post.comments.find_by_author_id(1) will return the comment with author_id 1 with the lowest id.

Comments

Leave a response

  1. Ian Lotinsky November 22, 2007 @ 01:26 AM

    Congrats—way to go!

Comment