Newbie questions

Hi all,

  I am new to Rails, and I have a few questions about it. I've done a
few tutorials, up to generating a scaffold using SQLite and inserting
some data.

1. Must I always create my tables using rake db:migrate? Or is it
possible to create a model using an existing table on an already
existing database? Can models be made manually?

2. How can I specify table relationships with the generate script? How
about more complex thing like CASCADE DELETE?

3. I am trying to generate a scaffold using mysql, but I get the
folowing error when I run db:migrate:

Mysql::Error: query: not connected: CREATE TABLE `schema_migrations`
(`version`
varchar(255) NOT NULL) ENGINE=InnoDB

(See full trace by running task with --trace)

I have installed the mysql gem, and my database is properly configured
in the config/database.yml file. The catalog specified already exists
on the MySQL server.

tuti plain wrote:

Hi all,

  I am new to Rails, and I have a few questions about it. I've done a
few tutorials, up to generating a scaffold using SQLite and inserting
some data.

1. Must I always create my tables using rake db:migrate? Or is it
possible to create a model using an existing table on an already
existing database? Can models be made manually?

Models are just ruby classes, so sure you can just make a model.
However, this is not the approach I would take. Personally, I would not
create a new Rails application from an existing database (unless there
was absolutely no other choice).

Chances are that the existing database is not going to be very Rails
friendly. Instead of using the existing database, I would instead use
the migrations and build a new database from scratch. I would then
create scripts to migrate the existing data into the new schema making
any necessary adjustments to the data to fit a more Rails friendly
database schema.

2. How can I specify table relationships with the generate script? How
about more complex thing like CASCADE DELETE?

You don't do that from generators. Database relations are not really
something that can be predicted correctly by generators. Fortunately,
associations in Rails are pretty straight forward and easy to add to
your model classes.

Rails associations act on the object level. If you want foreign key
constraints at the database level you need to add those to your database
migrations.

3. I am trying to generate a scaffold using mysql, but I get the
folowing error when I run db:migrate:

Mysql::Error: query: not connected: CREATE TABLE `schema_migrations`
(`version`
varchar(255) NOT NULL) ENGINE=InnoDB

(See full trace by running task with --trace)

I have installed the mysql gem, and my database is properly configured
in the config/database.yml file. The catalog specified already exists
on the MySQL server.

I don't know what's wrong here, but my first suggestions would be to
build a new empty rails application using MySQL:

rails my_proj --database mysql

Make sure you're database.yml is configured correctly for your MySQL
installation. Then generate a model. Once that's done run: rake
db:create. If that succeeds then run: rake db:migrate.

If any of that fails try running with the --trace as suggested in the
error you listed. Maybe that will point your in the right direction.