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

Miquel (a.k.a. Ktalà a.k.a. Ton)
Linux User #286784
GPG Key : 4D91EF7F
Debian GNU/Linux (Linux wolverine 2.6.23.1)

Welcome to the jungle, we got fun and games
Guns n’ Roses

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

Miquel (a.k.a. Ktalà a.k.a. Ton)
Linux User #286784
GPG Key : 4D91EF7F
Debian GNU/Linux (Linux wolverine 2.6.23.1)

Welcome to the jungle, we got fun and games
Guns n’ Roses

On Sat, 19 Apr 2008 09:20:50 +0200

On 19 Apr 2008, at 09:09, Miquel O. wrote:

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

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.

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

Regards,
Miquel

On Sat, 19 Apr 2008 10:53:57 +0100
Frederick C. wrote:

If this migration example the three tables will have a column named

 t.column :any_column, :string

Reinhart
http://teapoci.blogspot.com

Posted via http://www.ruby-forum.com/.

Miquel (a.k.a. Ktalà a.k.a. Ton)
Linux User #286784
GPG Key : 4D91EF7F
Debian GNU/Linux (Linux wolverine 2.6.23.1)

Welcome to the jungle, we got fun and games
Guns n’ Roses

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

Miquel (a.k.a. Ktalà a.k.a. Ton)
Linux User #286784
GPG Key : 4D91EF7F
Debian GNU/Linux (Linux wolverine 2.6.23.1)

Welcome to the jungle, we got fun and games
Guns n’ Roses

On Sat, 19 Apr 2008 02:52:08 -0700 (PDT)

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