Forum: Ruby on Rails Multiple foreign keys create / destroy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ruby N. (Guest)
on 2009-03-08 17:11
I have 4 models :

class Country < ActiveRecord::Base
  has_many :cities
  has_many :streets
  has_many :houses
end

class City < ActiveRecord::Base
  belongs_to :country
  has_many :streets
  has_many :houses
end

class Street < ActiveRecord::Base
  belongs_to :country
  belongs_to :cities
  has_many :houses
end

class House < ActiveRecord::Base
    belongs_to :country
    belongs_to :cities
    belongs_to :streets
end

Now...if i want to create a house like this:

@street = Street.find(1)
@house = @street.houses.new
@house.name = "Burlington house"
@house.save

Now...only the street_id key is set in the house record...But not
city_id and country_id...
Is there an ORM way to let active record do the job ?

Thanks
Frederick C. (Guest)
on 2009-03-08 17:42
(Received via mailing list)
On 8 Mar 2009, at 15:11, Ruby N. wrote:

> class City < ActiveRecord::Base
>
> class House < ActiveRecord::Base
>    belongs_to :country
>    belongs_to :cities
>    belongs_to :streets
> end

First off, watch those belongs_to - they should be singluar, ie
belongs_to :street.
> city_id and country_id...
> Is there an ORM way to let active record do the job ?
>
No. activerecord assumes the simple case ie that the has_many/
belongs_to relationship between street and house is all that it needs
(and why do you need to duplicate data between street and house ?
surely the country and city of the house is implicit from the street ?)

Fred
Ruby N. (Guest)
on 2009-03-08 17:54
Frederick C. wrote:
> On 8 Mar 2009, at 15:11, Ruby N. wrote:
>
>> class City < ActiveRecord::Base
>>
>> class House < ActiveRecord::Base
>>    belongs_to :country
>>    belongs_to :cities
>>    belongs_to :streets
>> end
>
> First off, watch those belongs_to - they should be singluar, ie
> belongs_to :street.
>> city_id and country_id...
>> Is there an ORM way to let active record do the job ?
>>
> No. activerecord assumes the simple case ie that the has_many/
> belongs_to relationship between street and house is all that it needs
> (and why do you need to duplicate data between street and house ?
> surely the country and city of the house is implicit from the street ?)
>
> Fred

Your post made it clear to me... Just a beginner...:(

Thanks
MaggotChild (Guest)
on 2009-03-09 07:35
(Received via mailing list)
> @house.name = "Burlington house"
> @house.save
>
> Now...only the street_id key is set in the house record...But not
> city_id and country_id...
> Is there an ORM way to let active record do the job ?

That is the ORM way. The problem lies in your design (i.e.
associations).

A country has many cities. Cities have many streets. These streets
contain houses.
Sure a house belongs to a city, but given the additional entities
involved the above model makes more sense.

@house = some_city.streets.find(1).houses.create(:name=>'Adelitas')
This topic is locked and can not be replied to.