Has_many :through questions

I have a database with 4 tables

Months has_many :weeks

Weeks has_many :days

Days has_many :hours

Hours

Weeks belong_to :month

Days belong_to :week

hours belong_to :day

I need to access the month_id in the Hour.rb model.

find(:first, :conditions => [“user_id = ? AND month_id = ?”, user_id,
month_id])

Thanks

On 12/6/06, G evans [email protected] wrote:

find(:first, :conditions => [“user_id = ? AND month_id = ?”, user_id,
month_id])

Where’s the :through from the subject line?

Anyway, this should work:

Hour.find(:first, :conditions=>[‘weeks.month_id=?’,month_id],
:include=>:week)

This instructs AR to add a join to the table from the hours table to
the weeks table. AR then makes the table available as ‘weeks’ for use
in the condition.

Cheers,
Max