Hi Guys,
I’ve been using Rails for the first time and have encountered some
issues that I’m not sure are a result of my inexperience with Rails or a
design oversight.
In my domain model, a School has an Address. In SQL, I’ve defined a
“schools” table and an “addresses” table, and the “schools” table
defines a column called “address_id” that refers to a row in the
“addresses” table.
When I model this using ActiveRecord, it seems that I’m meant to put a
line into the School model that looks like this:
class School < ActiveRecord::Base
belongs_to :address
end
and a line into the Address model that looks like this:
class Address < ActiveRecord::Base
has_many :schools
end
First of all, this seems unintuitive, since you don’t naturally think of
a School “belonging” to an Address, but the user documentation says that
the “belongs_to” clause should reside in the class whose table contains
the foreign key (in this case, “address_id”).
Second, when a School is destroyed I want its corresponding Address to
be automatically destroyed but I can’t do this using the above
declaration because the :dependent mechanism attaches to the :has_many
clause. In other words, I can arrange for a School to be destroyed when
its Address is destroyed, but not the other way around!
Am I missing something here, or is this a Rails design problem? If it’s
the latter, I highly recommend that it’s fixed before Rails goes to
version 1.0.
Regards,
Graham