# RoR data models ( extreme newbie question)

**URL:** <https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964>\
**Category:** Rails\
**Created:** [July 26, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964 "2006-07-26T12:27:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/1 "2006-07-26T12:27:07Z")

</div>

Sorry for asking this but I am keen to get on and build an app based on  
RoR and have had a look around at FAQa and the like with no immediate  
sucess.

What I am looking for is an explainantion of the rules/requirements for  
table/index/coulumn names with RoR. RoR appears to have a set of  
expected naming conventions which it would appear you can over ride but  
it would seems a hell of a lot easier if I could model my db to match  
what RoR wnats and he presto! I am in business.

If anyone could point me to such a page/doc I would appreciate it

Leggs

---

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/2 "2006-07-26T13:19:22Z")

</div>

I’m pretty new to Rails too and obivously I don’t know your previous  
experience but I’d recommend looking at the ‘Agile Web Devlopment with  
Rails’ book which you can find here.

[http://www.pragmaticprogrammer.com/titles/rails2/](http://www.pragmaticprogrammer.com/titles/rails2/)

It’s really helped me get started, and the fact that the PDF version is  
so cheap, there’s no reason not to get it!

Henry

---

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 1:25pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/3 "2006-07-26T13:25:53Z")

</div>

Martyn Bedford wrote:

> Sorry for asking this but I am keen to get on and build an app based on  
> RoR and have had a look around at FAQa and the like with no immediate  
> sucess.
> 
> What I am looking for is an explainantion of the rules/requirements for  
> table/index/coulumn names with RoR. RoR appears to have a set of  
> expected naming conventions which it would appear you can over ride but  
> it would seems a hell of a lot easier if I could model my db to match  
> what RoR wnats and he presto! I am in business.
> 
> If anyone could point me to such a page/doc I would appreciate it
> 
> Leggs

If you want to take advantage of the Rails framework, then you may name  
your database table names as something plural. For example if you have a  
database table named as departments then you should create a model class  
named as department. For more information visit

> **[Radar - O’Reilly](https://www.oreilly.com/radar/?page=2)**
>
> Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

regards,  
Prasad,  
Genie Interactive, Bangalore

---

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/4 "2006-07-26T14:05:41Z")

</div>

Hi Martyn,

The new way of creating db tables is to use migrations, but before we  
walk down that road, let me tell ya about the old way, which still works  
just fine.

Tables are always the plural name of the models they represent, so if  
you are going to have a user model then you have to have a users table  
and if you are going to have a product model you would name your db  
table products.

In order to make your life easier, and being the lazy programmer I am, I  
love easier, each table needs to have a primary key called id which is  
auto increment, an amazing ammount of rails automagic depends on this  
tiny step.

You can cruise on over to:  
[http://www.ocf.berkeley.edu/~jday/wiki/index.php?DevelopmentEnvironment](http://www.ocf.berkeley.edu/~jday/wiki/index.php?DevelopmentEnvironment)

and that should fill you in. Hope this helps.

Phil

---

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/5 "2006-07-26T14:31:23Z")

</div>

Phil P. wrote:

> Hi Martyn,
> 
> The new way of creating db tables is to use migrations, but before we  
> walk down that road, let me tell ya about the old way, which still works  
> just fine.
> 
> Tables are always the plural name of the models they represent, so if  
> you are going to have a user model then you have to have a users table  
> and if you are going to have a product model you would name your db  
> table products.
> 
> In order to make your life easier, and being the lazy programmer I am, I  
> love easier, each table needs to have a primary key called id which is  
> auto increment, an amazing ammount of rails automagic depends on this  
> tiny step.
> 
> You can cruise on over to:  
> [http://www.ocf.berkeley.edu/~jday/wiki/index.php?DevelopmentEnvironment](http://www.ocf.berkeley.edu/~jday/wiki/index.php?DevelopmentEnvironment)
> 
> and that should fill you in. Hope this helps.
> 
> Phil

Thanks everyone for your answers, as with any answer it invenitably  
leads to another question.

So if I have my object I create a table called object(s) and create a  
primary key called id, so good so far, now what I need to do is create  
joins and foriegn keys to other tables, I have seen this working in a  
few examples but I am lost as to the naming conventions for this.

Martyn

---

<div class="post-metadata">

**Author:** ![leggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/df788c/32.png) [@leggs](https://www.ruby-forum.com/u/leggs)\
**Post date:** [July 26, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/ror-data-models-extreme-newbie-question/99964/6 "2006-07-26T15:37:57Z")

</div>

Martyn Bedford wrote:

> So if I have my object I create a table called object(s) and create a  
> primary key called id, so good so far, now what I need to do is create  
> joins and foriegn keys to other tables, I have seen this working in a  
> few examples but I am lost as to the naming conventions for this.

This is pretty easy —

There are two basic ways to relate in ruby has\_many or has\_one and  
belongs\_to. As always, there are options to handle complex cases.

Check the api docs for these functions and much will become clear.  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/)

Basically the difference is where the foreign key lives. In belongs\_to,  
it is in the table for the model.

For example:

If you have a cities table and your object belongs\_to a :city, you will  
have a city\_id field in the objects table. Note the plural/singluar -  
the table is plural but the foreign key is singluar.

If you have a has\_many or has\_one relationship then the foreign key  
lives in the other table. So to take the example further, in the City  
model definition, you would put “has\_many :objects” – no futher work  
needed. Now the following should be true for any city with at least one  
object: city.objects[0].city == city.

Steven
