How to best start from scratch implementing a ERM diagram?

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!

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.