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.