pragmatist
Patrick Joyce

November 21, 2007

I'm a Rails Contributor!

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.

More Articles on Software & Product Development

Agile With a Lowercase “a”
”Agile“ is an adjective. It is not a noun. It isn’t something you do, it is something you are.
How Do You End Up With A Great Product A Year From Now?
Nail the next two weeks. 26 times in a row.
Build it Twice
Resist the urge to abstract until you've learned what is general to a class of problems and what is specific to each problem.