Hi there

Hi there

I subscribed to this mailint list this morning after days looking for
the way to do it, so this is my first post.

My name is Miquel and I have been coding with ruby for a year and a
half (using Qtruby bindings) and now I'm a rails newbie (I'm with it
since a month ago). Now I'm coding an app and I'm trying to create
(using migrations) a table which has a column named 'id' which is
primary key but it's not auto_increment. The primary key db schema of
this part is:

Table users: 'id' primary key auto_increment.
Table registered_users: 'id' primary key.
Table non_registered_users: 'id' primary key.

I migrated it using the execute command to create the table first and
after using the add_column command.

My question is...is there a way using rails migrations to create
these three tables?

Thanks in advance.

Regards

class CreateManyTable < ActiveRecord::Migration
  def self.up
    create_table :first_tables do |t|
      t.column :any_column, :string
    end
    create_table :second_tables do |t|
      t.column :any_column, :string
    end
    create_table :third_tables do |t|
      t.column :any_column, :string
    end
  end

  def self.down
    drop_table :first_tables
    drop_table :second_tables
    drop_table :third_tables
  end
end

Reinhart
http://teapoci.blogspot.com

Maybe I didn't make myself clear (english problem maybe). The thing is
that I want that the first table's (first_tables) id column is
auto_increment and not the second and the third one (second_tables and
third_tables).

If this migration example the three tables will have a column named
'id' which is integer, primary_key and auto_increment.

Regards

Why would you want the ID column not to be auto_increment?

On Rails you don't necessarily code towards specific values on
columns, you code towards objects and their properties.

And the table names you mentioned: users, registered_users,
non_registered_users suggest that you should have one user model with
a column registered, type boolean (instead of three different models).
Then, if you want to find all objects from that model which have the
property registered as false, you do something like:

@users = Users.find(:all, :registered => true)

And note that I don't use tables and columns to refer to data, I use
models and properties. You should code in Rails from that
perspective.

Maybe I didn't make myself clear (english problem maybe). The thing is
that I want that the first table's (first_tables) id column is
auto_increment and not the second and the third one (second_tables and
third_tables).

If this migration example the three tables will have a column named
'id' which is integer, primary_key and auto_increment.

You can pass :id => false to create_table to stop it creating the id
column for you (you can then create it yourself with whatever options
you want)

Fred

Hi

Sometimes user's stored data is different if a user is registered or
not (maybe a registered user has email, login, password, etc and the
non_registered user only has the email). In this case you have two
options.

The first one is add all the column in an only table inserting null
values depending if its a non registered user, but I don't like this
way. The other option is a table called users and two tables, which has
a relation 1:1 with the previous one called registered and
non_registered. The tables users has the common data which have
registered and non_registered and the different data is stored in the
other two tables.

The column 'id' is common to the three tables but in table users is
autonumeric and in the other two is not (because the id value is set
by the first one and share with the others).

Hope this explains better.

Regards

Thanks Fred, it seems to be the right answer. I'm going to try it.

Regards,
Miquel