# Association \> Correct way?

**URL:** <https://www.ruby-forum.com/t/association-correct-way/146368>\
**Category:** Rails\
**Created:** [August 20, 2008, 11:39am UTC](https://www.ruby-forum.com/t/association-correct-way/146368 "2008-08-20T11:39:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [August 20, 2008, 11:39am UTC](https://www.ruby-forum.com/t/association-correct-way/146368/1 "2008-08-20T11:39:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [August 20, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/association-correct-way/146368/2 "2008-08-20T12:16:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [August 20, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/association-correct-way/146368/3 "2008-08-20T15:50:03Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [August 20, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/association-correct-way/146368/4 "2008-08-20T22:09:20Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
