Confuse with AR association

Dear all

Refer to rails.info

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

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. [email protected]

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/
Learn rails: http://sensei.zenunit.com/

On 12/02/2009, at 3:19 AM, Valentino L.
<[email protected]

Valentino L. wrote:

Dear all

Refer to rails.info

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.