Rails Naming Conventions


#1

DB field names:

If I have a table that references 2 or more separate users from my users
table, is there a recommended naming convention for this situation? In
my case, I have 3 users associated with a record in my projects table:
requester_user_id
created_by
updated_by

I could name one of them “user_id”, and then projects.user would work I
guess, but wouldn’t work for the others. Is there a recommendation for
this kind of situation?

Plurality in Classes:
Not in class names as much as in the part where you say belongs_to,
has_many, etc. Say all my table names are plural in the db. do I say:
has_many :languages
has_one :user

or
has_many :language
has_one :user

or
has_many :languages
has_one :users

In other words, do I need to set the right plurality here, or can I just
point to the exact name of the field in the DB? I’m getting lots of
errors like:
undefined method `vendor_projects’ for ProjectTranslation:Class

And I’m not sure if that’s related or not.


#2

Micah B. wrote:

DB field names:

If I have a table that references 2 or more separate users from my users
table, is there a recommended naming convention for this situation? In
my case, I have 3 users associated with a record in my projects table:
requester_user_id
created_by
updated_by

I could name one of them “user_id”, and then projects.user would work I
guess, but wouldn’t work for the others. Is there a recommendation for
this kind of situation?

Plurality in Classes:
Not in class names as much as in the part where you say belongs_to,
has_many, etc. Say all my table names are plural in the db. do I say:
has_many :languages
has_one :user

or
has_many :language
has_one :user

or
has_many :languages
has_one :users

In other words, do I need to set the right plurality here, or can I just
point to the exact name of the field in the DB? I’m getting lots of
errors like:
undefined method `vendor_projects’ for ProjectTranslation:Class

And I’m not sure if that’s related or not.

Is correct, and sounds better if read out.
has_many :languages
has_one :user

In your project model put:
belongs_to :requester_user, :foreign_key => ‘request_user_id’,
:class_name => ‘User’
belongs_to :created_by, :foreign_key => ‘created_by’, :class_name =>
‘User’
belongs_to :updated_by, :foreign_key => ‘updated_by’, :class_name =>
‘User’

Then you’ll be able to do project.request_user, project.created_by and
project.updated_by

Hope this helps
Joey__
http://www.feedreed.com


#3

joey__ wrote:

Is correct, and sounds better if read out.
has_many :languages
has_one :user

so… what are those names you are putting behind the :? I thought at
first they were table names, but since we can change them with
plurality/singularity, they can’t be table names per se??

It just seems weird to refer to something one place as “has_one:
language” and in other place as “has_many: languages”, when we’re
talking about the same table.

In your project model put:
belongs_to :requester_user, :foreign_key => ‘request_user_id’,
:class_name => ‘User’
belongs_to :created_by, :foreign_key => ‘created_by’, :class_name =>
‘User’
belongs_to :updated_by, :foreign_key => ‘updated_by’, :class_name =>
‘User’

Then you’ll be able to do project.request_user, project.created_by and
project.updated_by

Joey,

That helps a great deal, thank you! works like a charm…

Micah


#4

so… what are those names you are putting behind the :? I thought at
first they were table names, but since we can change them with
plurality/singularity, they can’t be table names per se??

The use of : indicates ‘pointers’. Sometimes they used as key names in
hash tables ie :key => value. I think the Pickaxe book says to think of
them as pointing to the variable (NB rather than the value of it)

In this usage the type of the relationship will indicate what the point
to - a child table, a join table etc

So whilst has_one :language will point to language_id in parent table,
has_and_belongs_to_many :languages with point to the collection of
language_id 's in the languages_users join table.

_Tony