Where is the belongs_to :town in the Street Model?

class Town < ActiveRecord::Base
  has_many :streets
  has_many :houses, :through => :streets
end

class Street < ActiveRecord::Base
  belongs_to :town
  has_many :houses
end

You can then do Town.first.houses just fine.

On Sep 9, 4:25 pm, steven_noble <ste...@snoble.net> wrote:
> OK, I added a town_id to each house and it's working fine, but if
> there's a method I've missed that handles this automagically, it'd be
> great to know.
>
> s.
>
> On Sep 8, 10:21 pm, steven_noble <ste...@snoble.net> wrote:
>
>
>
> > Let's say...
>
> > class Town < ActiveRecord::Base
> >   has_many :streets
> >   has_many :houses, :through => :streets
> > end
>
> > class Street < ActiveRecord::Base
> >   has_many :houses
> > end
>
> > Then we can find individual streets and houses like this...
>
> > @first_town = Town.find(:first)
> > @first_towns_first_street = @first_town.streets.first
> > @first_streets_first_house = @first_town_first_street.house.first
>
> > We can also find arrays of streets and houses like this...
>
> > @first_town_all_streets = @first_town.streets
> > @first_street_all_houses = @first_street.houses
>
> > But what is the correct syntax for the kind of array I'm trying to
> > generate here?
>
> > @first_town_all_houses = @first_town.streets.houses
>
> > I'm presuming I can create a methods that iterates through the streets
> > array, generates an array of houses for each street, and adds that
> > array to a larger array of my own creation. Likewise, I'm assuming I
> > can assign a town_id to each house whenever I create it, just as I
> > automatically assign it a street_id through create/build. However, I'm
> > hoping there's an existing method that'll just magic up the results.
> > Problem is, so far I haven't found it on either of the following
> > pages:
>
> >http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet...
>
> > Thanks in advance,
>
> > Steven.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to