On 12/6/06, G evans
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
> 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])
>
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---