# Rails Naming Conventions

**URL:** <https://www.ruby-forum.com/t/rails-naming-conventions/73379>\
**Category:** Rails\
**Created:** [February 26, 2006, 4:37am UTC](https://www.ruby-forum.com/t/rails-naming-conventions/73379 "2006-02-26T04:37:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b3f665/32.png) [@yak](https://www.ruby-forum.com/u/yak)\
**Post date:** [February 26, 2006, 4:37am UTC](https://www.ruby-forum.com/t/rails-naming-conventions/73379/1 "2006-02-26T04:37:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b3f665/32.png) [@yak](https://www.ruby-forum.com/u/yak)\
**Post date:** [February 26, 2006, 11:22am UTC](https://www.ruby-forum.com/t/rails-naming-conventions/73379/2 "2006-02-26T11:22:01Z")

</div>

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](http://www.feedreed.com)

---

<div class="post-metadata">

**Author:** ![yak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b3f665/32.png) [@yak](https://www.ruby-forum.com/u/yak)\
**Post date:** [February 26, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/rails-naming-conventions/73379/3 "2006-02-26T17:20:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b3f665/32.png) [@yak](https://www.ruby-forum.com/u/yak)\
**Post date:** [February 26, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/rails-naming-conventions/73379/4 "2006-02-26T18:18:52Z")

</div>

> 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
