RoR data models ( extreme newbie question)

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

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/

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

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
http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=2

regards,
Prasad,
Genie Interactive, Bangalore

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

and that should fill you in. Hope this helps.

Phil

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

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

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/

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs