drop table if exists articles; create table articles ( id int not null auto_increment, title varchar(100) not null, description text null, image_url varchar(200) not null, price decimal(10,2) not null, date datetime not null, vender varchar(100) not null, category varchar(100) not null, primary key (id) );
ruby script/generate scaffold article title:string description:text image_url:string price:decimal date:datetime vender:string category:string実行例-rubyonrails-createtable?
ruby script/generate scaffold article title:string description:text image_url:string price:decimal date:datetime vender:string category:stringがそのまま使えるかも
%cd db/migrate/ %ls >001_create_articles.rb
001_create_articles.rb?
データベースにこのテーブルを適用する場合には、このupメソッドを使います。
目的のテーブルを定義するコードはこの部分に記述します。
今回は、scaffoldを利用してフィールドをある程度定義しましたので、すでにupメソッドにはフィールドが追加されています。
ただ、priceフィールドの型「decimal」を10桁、小数点第2位の数字に定義したいので、「001_create_articles.rb」ファイルを以下のように修正します。
001_create_articles.rb#modefied?
このように、「t.decimal : price, : precision => 10, : scale => 2, : default =>0」とすると、「decimal」を10桁、小数点第2位の数字でかつ、デフォルトの値を「0」で定義できます。
%rake db:migrate >(in /home/ono/rails_study/office) == 1 CreateArticles: migrating ================================================ -- create_table(:articles) -> 0.0044s == 1 CreateArticles: migrated (0.0047s) =======================================これでいいっぽい。
%sqlite3 office_development.db sqlite> .schema CREATE TABLE articles( id INTEGER not null PRIMARY KEY AUTOINCREMENT, title varchar(100) not null, description text null, image_url varchar(200) not null, price decimal(10,2) not null, date datetime not null, vender varchar(100) not null, category varchar(100) not null);
%ruby script/server
実行例?
このままだと、ポート番号3000で起動する。
先生が3000で使っていたからか、変になった。
%ruby script/server -p 5666
で実行する。
=> Booting WEBrick... => Rails application started on http://0.0.0.0:5666 => Ctrl-C to shutdown server; call with --help for options
http://localhost:5666/articles/
例:
#ref(): File not found: "RubyOnRails-ex01.png" at page "RailsOffice/081014"
exists app/models/ exists app/controllers/ exists app/helpers/ exists app/views/articles exists app/views/layouts/ exists test/functional/ exists test/unit/ overwrite app/views/articles/index.html.erb? (enter "h" for help) [Ynaqdh]
forcing scaffold force app/views/articles/index.html.erb force app/views/articles/show.html.erb force app/views/articles/new.html.erb force app/views/articles/edit.html.erb identical app/views/layouts/articles.html.erb identical public/stylesheets/scaffold.css dependency model exists app/models/ exists test/unit/ exists test/fixtures/ identical app/models/article.rb identical test/unit/article_test.rb skip test/fixtures/articles.yml exists db/migrate Another migration is already named create_articles: db/migrate/001_create_articles.rb
exists db/migrate create db/migrate/002_remove_date.rb
%rake db:migrate (in /home/ono/rails_study/office) == 2 RemoveDate: migrating ==================================================== -- remove_column(:articles, :date) -> 0.0257s == 2 RemoveDate: migrated (0.0261s) ===========================================
sqlite> .schema articles CREATE TABLE articles( id INTEGER not null PRIMARY KEY AUTOINCREMENT, title varchar(100) not null, description text null, image_url varchar(200) not null, price decimal(10,2) not null, date datetime not null, vender varchar(100) not null, category varchar(100) not null);消えてなかった・・・・
#ref(): File not found: "RubyOnRails-Articles-remove.png" at page "RailsOffice/081014"
class AddDate < ActiveRecord::Migration def self.up add_column :articles, :date, :datetime end def self.down remove_column :articles, :date end end