# Confuse with AR association

**URL:** <https://www.ruby-forum.com/t/confuse-with-ar-association/160087>\
**Category:** Rails\
**Created:** [February 11, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/confuse-with-ar-association/160087 "2009-02-11T17:19:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 11, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/confuse-with-ar-association/160087/1 "2009-02-11T17:19:01Z")

</div>

Dear all

Refer to [rails.info](http://guides.rails.info/association_basics.html)

Why :physician and :patient some with (s), and some not…? It actually  
refer to the class name or table name? \*\* really confuse \>\_\<. Can  
someone explain it? Is it a convention of rail? Thank you.

class Physician \< ActiveRecord::Base  
has\_many :appointments  
has\_many :patients, :through =\> :appointments  
end

class Appointment \< ActiveRecord::Base  
belongs\_to :physician  
belongs\_to :patient  
end

class Patient \< ActiveRecord::Base  
has\_many :appointments  
has\_many :physicians, :through =\> :appointments  
end

Many thanks  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 11, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/confuse-with-ar-association/160087/2 "2009-02-11T17:45:20Z")

</div>

It is the convention in Rails  
A Physician (singular) has many appointments (plural)  
An Appointment (singular) belongs to one patient (singular)

2009/2/11 Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 12, 2009, 2:22am UTC](https://www.ruby-forum.com/t/confuse-with-ar-association/160087/3 "2009-02-12T02:22:58Z")

</div>

It depends whether there are more than one or not. A class is the  
blueprint of an instance object. Thus a physician class describes one  
object, of class physician. Has many means one physician has a  
collection of some other kind of object. Belongs to means a physician  
can be part of  
A collection of physicians for another class of object and is  
therefore the other ‘side’ if you will.

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 12/02/2009, at 3:19 AM, Valentino L.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 11, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/confuse-with-ar-association/160087/4 "2009-02-11T19:45:41Z")

</div>

Valentino L. wrote:

> Dear all
> 
> Refer to [rails.info](http://guides.rails.info/association_basics.html)
> 
> Why :physician and :patient some with (s), and some not…? It actually  
> refer to the class name or table name? \*\* really confuse \>\_\<. Can  
> someone explain it? Is it a convention of rail? Thank you.

My guess is that English is not your native language. This might be why  
you are confused by the Rails conventions, which follows English  
singular/plural conventions.

Here’s the scoop. I hope you can follow along:

First let’s look at a Rails model object. The model class is like a  
prototype (or template) representing a single row in a database table.  
This is why the model class name is singular.

Example:  
class Physician \< ActiveRecord::Base  
…  
end

The database table that stores each physician contains a collection of  
physicians, which is why the table name is plural.

Rails associations can represent either one or many model objects.

Associations described as either has\_one or belongs\_to represent one  
object and therefore use the singular form:

Examples:  
class Appointment \< ActiveRecord::Base  
has\_one :physician # This is one Physician object (singular)  
end

class Appointment \< ActiveRecord::Base  
belongs\_to :physician # This is one Physician object (singular)  
end

Note: Notice that has\_one is used for one side of a one-to-one  
association. There other side would use belongs\_to.

Example:  
class Unicycle \<\< ActiveRecord::Base  
has\_one :wheel  
end

class Wheel \<\< ActiveRecord::Base  
belongs\_to :unicycle  
end

Associations described as has\_many refer to a collection (array) of  
objects and therefore use the plural form:

Examples:  
class Physician \< ActiveRecord::Base  
has\_many :appointments # This is an array of Appointment objects  
(plural)  
end

I hope this makes things a little more clear to you.
