About Me

My Photo
Kevin Compton
Entreprenuer and professional software developer. Husband, Father and Friend.
View my complete profile

Sunday, November 30, 2008

An issue w/ rails association extensions inside a namespaced model

In Rails 2.1, a co-worker and I recently discovered that there is an issue with creating an association extension method within a namespaced model.
I have not gotten into exactly why it doesn't work but I can share with you a work around.

First, here is the code that will fail:

/project/app/models/financial/budget.rb

class Financial::Budget < ActiveRecord::Base
has_many :charge_sets do
def by_category
self.group_by(:category)
end
end
end
However, we found that having the has_many extend a module still works:

class Financial::Budget < ActiveRecord::Base
module ByCategory
def by_category
self.group_by(&:category) # group_by in the array of arrays sense *not* the sql sense
end
end
has_many :charge_sets, :extend => ByCategory
end


BTW, my coworker has a great blog that I have found useful in the past perhaps you will too!
Avdi Grimm http://avdi.org/devblog/

0 comments: