# Can rails create a database table

**URL:** <https://www.ruby-forum.com/t/can-rails-create-a-database-table/84550>\
**Category:** Rails\
**Created:** [February 5, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/can-rails-create-a-database-table/84550 "2007-02-05T23:08:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [February 5, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/can-rails-create-a-database-table/84550/1 "2007-02-05T23:08:43Z")

</div>

Hi,

I know the activeRecords can use easily a table from a database, but if  
I want to create a new table, can rails do this???

Thanks you very much

Sayoyo

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [February 5, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/can-rails-create-a-database-table/84550/2 "2007-02-05T23:16:28Z")

</div>

> I know the activeRecords can use easily a table from a database, but if  
> I want to create a new table, can rails do this???
> 
> Thanks you very much

Yes, it can.

[http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html](http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html)

---

<div class="post-metadata">

**Author:** ![sayoyo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba8739/32.png) [@sayoyo](https://www.ruby-forum.com/u/sayoyo)\
**Post date:** [February 5, 2007, 11:49pm UTC](https://www.ruby-forum.com/t/can-rails-create-a-database-table/84550/3 "2007-02-05T23:49:57Z")

</div>

sayoyo wrote:

> I know the activeRecords can use easily a table from a database, but if  
> I want to create a new table, can rails do this???

Yes, you can do this by using a migration.

Once you have created a database, such as bookstore\_development, you can  
create a table in it very easily by typing this:

ruby script/generate migration books

This will generate a folder and a file, or just a file if the folder  
already exists. (I use a PC, but for Unix-based systems I think you can  
leave out the “ruby”). You’ll see something like this:

exists db/migrate  
create db/migrate/001\_books.rb

Open the the 001\_books.rb file and you’ll see this:

class Books \< ActiveRecord::Migration  
def self.up  
end

def self.down  
end  
end

Add the columns you want in your books table like this:

class Books \< ActiveRecord::Migration  
def self.up  
create\_table :books  
t.column :title, :string  
t.column :author, :string  
end  
end

def self.down  
drop\_table :books  
end  
end

Then, if you type the following:

rake db:migrate

the table will be created with the two columns. That’s it!

If you then type:

rake db:migrate version=0

The table will be dropped from the database.

Once you’ve experimented with that you might want to try typing:

rake db:migrate version=0

and:

ruby script/destroy migration books

to delete the files you just generated and then type:

ruby script/generate model book

This will generate the same folder and file plus some other stuff too.  
That’s probably more useful in the short term than just producing a  
migration file. The migration file will also be more filled-in, so  
you’ll have to type less.

Notice that the word after “model” in that command should be singular.  
This will still produce some files called “books” in the plural,  
including the migration file.

I’m pretty new to this myself, so someone may correct what I’ve written!
