Starting my first application with Rails

Hi:

I’m starting a simple application to learn Rails and Ruby of course. I come from PHP and frameworks like Symfony, CakePHP and CodeIgniter and some of them have a task to create DB models from existent database which is my case. I have a PostgreSQL database which want to use in my app. My question is: exists any rake or rails command to generate models if they exists in Rails?

Ing. Reynier Perez Mira eMail: reynierpm@gmail.com, reynierpm@hotmail.com Skype: reynierpm

Mobile: +58 424.180.5609 Site: http://reynierpm.site90.com

A model can be generated using

rails generate model MODELNAME field1:type field2:type …

Javier Q.

Thanks Javier and what about ID (PK) fields? Need to be specified too or Rails is smart enough to get them from tables?

Ing. Reynier Perez Mira eMail: reynierpm@gmail.com, reynierpm@hotmail.com

Skype: reynierpm Mobile: +58 424.180.5609 Site: http://reynierpm.site90.com

Also can my tables be named in singular or all needs to be plural? I’m reading conventions right now but have this doubt. For example if I have a table called record the correct name for the model could be Record << ActiveRecord::Base?

Ing. Reynier Perez Mira eMail: reynierpm@gmail.com, reynierpm@hotmail.com Skype: reynierpm Mobile: +58 424.180.5609

Site: http://reynierpm.site90.com

Thanks Javier and what about ID (PK) fields? Need to be specified too or Rails is smart enough to get them from tables?

To be clear, the instructions that Javier gave you are to create a new table and model to match, not to introspect an existing database table and infer a model from it. This instruction is precisely how you create a new model when starting a green-field Rails application.

To answer your follow up question, yes, Rails will add id, created_at and updated_at columns (int-11, datetime, datetime) automagically. You can certainly follow these steps (and be sure to issue the --skip-migrations flag at the end so you don't end up with a redundant migration file) but I don't think this is actually the answer you were looking for. I don't have one for you either, sorry, but I didn't want you to go down this path and get confused when it didn't do what you asked for.

Walter

When you generate for example

rails generate model Projects name:string

it will generate a table

id name created_at updated_at

and as you see I named it PROJECTS but rails is smart enough to change it into PROJECT =)

You should give this tutorial a try

http://ruby.railstutorial.org/

and also

http://railsforzombies.org/

Hope that helps

Javier Q.

Also can my tables be named in singular or all needs to be plural? I'm reading conventions right now but have this doubt. For example if I have a table called record the correct name for the model could be Record << ActiveRecord::Base?

You can certainly override the conventions, but it's going to be messy. Particularly if you do something like tell Rails that the plural of record is record. The conventions are there to remove needless configuration.

Walter

Also can my tables be named in singular or all needs to be plural? I'm reading conventions right now but have this doubt. For example if I have a table called record the correct name for the model could be Record << ActiveRecord::Base?

You can do that sort of thing (see set_table_name), however I strongly suggest that first you work right through a good tutorial such as railstutorial.org (which is free to use online) in order to get a grasp of the basics. Rails relies for much of its magic on sticking to the conventions. It /is/ possible to circumvent them but life is much easier if you can avoid it. If you google for rails legacy database you will find a lot of useful stuff, but again if you can possibly change the db schema to match rails conventions then do that.

Colin

@Javier: Thanks for your clarifications it’s a bit clear to me now

@Colin: I can do that without problem because I’m starting the application so maybe I could create models from scratch using Rails but what about fixtures? I’ve a table country and will be nice if I can populate the table with 238 records :slight_smile:

@Walter: Understood but what about relations? How to deal with this from rails command line? I have relations BelongsTo (record belongs to country) and hasMany (record hasMany attachment and some others)

Thanks to every
Ing. Reynier Perez Mira eMail: reynierpm@gmail.com, reynierpm@hotmail.com

Skype: reynierpm Mobile: +58 424.180.5609 Site: http://reynierpm.site90.com

@Javier: Thanks for your clarifications it's a bit clear to me now @Colin: I can do that without problem because I'm starting the application so maybe I could create models from scratch using Rails but what about fixtures? I've a table country and will be nice if I can populate the table with 238 records :slight_smile:

As I suggested previously, work through railstutorial.org and it will introduce you to a variety of techniques including testing options. Fixtures are not considered the best way of testing by most developers now.

@Walter: Understood but what about relations? How to deal with this from rails command line? I have relations BelongsTo (record belongs to country) and hasMany (record hasMany attachment and some others)

The same goes here, work through the tutorial and you will be able to answer basic questions yourself.

Colin