A "ruby way" to build a sql-aggregate accessor?


#1

HI all,

I have an AR doing the following :

class Home < ActiveRecord::Base

has_many :home_milestone

def home_milestone_last_update
hm = Home::find_by_sql [“SELECT MAX(updated_on) as last_update FROM
home_milestones WHERE home_id = ?”, id]
return hm[0].last_update unless hm.size == 0
end #def home_milestone_last_update

end

This give me usage :
irb> h.home_milestone_last_update
=> “2005/11/21 16:02:46”

What i’d like to have is this usage (notice the ‘.’ instead of ‘_’ :

irb> h.home_milestone.last_update
=> “2005/11/21 16:02:46”

Does my request place the functionality under the HomeMilestone class
then?
What does that look like? Am I walking downt the path to insanity?

Thanks all.


#2

On Monday 21 November 2005 22:08, Peter F. wrote:

home_milestones WHERE home_id = ?", id]

irb> h.home_milestone.last_update
=> “2005/11/21 16:02:46”

Does my request place the functionality under the HomeMilestone class
then? What does that look like? Am I walking downt the path to
insanity?

Make a Milestone class?