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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---