Forum: Ruby on Rails How to best start from scratch implementing a ERM diagram?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2008-10-17 12:58
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!
9b4c04c050122bcea16a6f3376d680fa?d=identicon&s=25 Daniel Bush (danb)
on 2008-10-17 15:06
Heinz Strunk 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.

<soapbox subject="testing" >
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.
</soapbox>

[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 Bush
This topic is locked and can not be replied to.