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/

Its a start!

This blog will serve as a place for me to share lessons learned while I pursue my dream of being a successful entrepreneur.

I have a degree in business and have recently started my first LLC -- Grid Iron Teams, LLC.
The plan for Grid Iron Teams is to provide online communication & planning tools for football teams.

My wife and I have started a business in the past but did not fully commit ourselves to working it. We have a commercial embroidery machine, heat transfers, designs and software to show for it. Perhaps we will be able to utilize these assets to provide promotional merchandise for Grid Iron Teams.

I am also a software developer with over a decade of professional experience.

In this blog, I will share lessons & tips I learn as I continue to progress in the software and business arena(s).

Currently, I develop primarily in Ruby so many of my posts may be more specific to Ruby. I utilize the Rails & Merb frameworks... so posts may be related to them.

Thanks for checking out the blog. I hope to provide value in my posts ... hopefully we can grow and learn from each other.