# How to best start from scratch implementing a ERM diagram?

**URL:** <https://www.ruby-forum.com/t/how-to-best-start-from-scratch-implementing-a-erm-diagram/151155>\
**Category:** Rails\
**Created:** [October 17, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/how-to-best-start-from-scratch-implementing-a-erm-diagram/151155 "2008-10-17T12:58:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [October 17, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/how-to-best-start-from-scratch-implementing-a-erm-diagram/151155/1 "2008-10-17T12:58:54Z")

</div>

Hey people,

I’m starting a new project and just finished creating the ERM diagram  
with about 30 tables. There are quite some tables with foreign keys so  
I’m not sure how to start creating the controllers/models.  
How should I start creating controllers/models? Should I first create  
all tables without foreign key? Should I just start somewhere and add  
them radomly?

I’m also not sure which table should become a controller and which one  
only a model. How should I decide that? If there’s no real ‘rule’ to  
decide this… could I just create all of them as a model and if I need  
one to be a controller I just create the same on as a controller without  
overwriting the model.rb?

Thanks!

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [October 17, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/how-to-best-start-from-scratch-implementing-a-erm-diagram/151155/2 "2008-10-17T15:06:34Z")

</div>

Heinz S. wrote:

> Hey people,
> 
> I’m starting a new project and just finished creating the ERM diagram  
> with about 30 tables. There are quite some tables with foreign keys so  
> I’m not sure how to start creating the controllers/models.  
> How should I start creating controllers/models? Should I first create  
> all tables without foreign key? Should I just start somewhere and add  
> them radomly?

Identify all the has\_many and has\_one associations from your diagram;  
set up your foreign keys as per the rails naming conventions (eg table  
‘users’ id primary key maps to ‘user\_id’ field in another table - that  
sort of thing). Some people will also put in foreign key constraints  
too - especially if the system is being updated outside of rails.  
You may also have things like ‘has\_many :through’ for many-to-many  
relationships; and other more advance things I won’t mention here.  
By mapping out the has\_\* assocations, you will also know pretty much  
most of your belongs\_to associations by extension.

Then script/generate your AR models and wire them up with their  
associations. Migrate the development database; clone the test database  
from the development database.

I’d then head over to test/unit[1] and test/fixtures and start testing  
your models: create some test data in test/fixtures and flesh out what  
business/app logic they are going to encapsulate.

Sounds like you're newish to rails so doing stuff test first will be hard. However, it's a great way to actually use, refine and play with what you're building and the most straight forward part of rails to test because you don't have to worry about http, redirects, templates etc If you know your models are rock solid, then that's one less thing to worry about when you are dealing with your controllers and templates.

[1] Seems like a lot of people are using rspec now; I’m just referring  
here to the older test unit framework that is still the default in rails

> I’m also not sure which table should become a controller and which one  
> only a model. How should I decide that? If there’s no real ‘rule’ to  
> decide this… could I just create all of them as a model and if I need  
> one to be a controller I just create the same on as a controller without  
> overwriting the model.rb?

Don’t know what you mean by all of this. Controllers are there to  
handle all the http stuff: what template to render, what http status  
code to return to the browser, sending messages to your models, how to  
handle exceptions from the models or elsewhere etc…

HTH, if you’re new to rails read some tutorials or get the rails book.

–  
Daniel B.
