Association > Correct way?

Hi,

newbie-question:

2 tables
countries
countrydescription (additional info > metadata etc. )

Models

class countries < ActiveRecord::Base
has_one :countrydescription
end

class countrydescription < ActiveRecord::Base
has_one :country
end

Is this the correct way?
And how can i show the countrydescription.metatitle on the country page
in the show.rhtml?

Grtz…remco

On 20 Aug 2008, at 10:39, Remco S. wrote:

class countries < ActiveRecord::Base
has_one :countrydescription
end

class countrydescription < ActiveRecord::Base
has_one :country
end

Is this the correct way?

Nope. assuming it’s a 1 to 1 relation ship, one of those has_ones
should be a belongs_to (the one where the foreign key lives)

Fred

First - your models should be singularly named. Use Country instead
of Countries, as your model is of a single country not a group of
countries.

class Country < ActiveRecord::Base
has_one :countrydescription
end

class CountryDescription < ActiveRecord::Base
belongs_to :country
end

I’d be curious why you’re storing the description of the country in a
separate table instead of along with the Country model? Even if it’s
meta data I would think this is better kept with the Country model,
and just have a text field called “description” it’d keep things
simpler.

Nicholas

On Aug 20, 4:39 am, Remco S. [email protected]

Also, You should consider fixing your naming to match the Ruby/Rails
naming conventions.

class Country < ActiveRecord::Base
has_one :countrydescription
end

class CountryDescription < ActiveRecord::Base
belongs_to :country
end
This reply was almost right, but I think it should be:
has_one :country_description

Class names should be camel case with the first character upper case and
the each word upper.

Example: SomeClassName

Attribute names and method names should be all lower case with
underscores separating each word (consider table column names to be
attributes).

Example: some_attribute_name

ncancelliere wrote:

First - your models should be singularly named. Use Country instead
of Countries, as your model is of a single country not a group of
countries.

class Country < ActiveRecord::Base
has_one :countrydescription
end

class CountryDescription < ActiveRecord::Base
belongs_to :country
end

I’d be curious why you’re storing the description of the country in a
separate table instead of along with the Country model? Even if it’s
meta data I would think this is better kept with the Country model,
and just have a text field called “description” it’d keep things
simpler.

Nicholas

On Aug 20, 4:39�am, Remco S. [email protected]