私はruby-on-railsの初心者です。レーキに疑問があります。create
関数は新しいデータベースを作成することです。その後、次のようなコマンドを実行します
rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed
しかし、dbを作成した後にこのcmdを実行する理由と、aboutcmdの機能について説明します。
アドバイスありがとうございます。
私はruby-on-railsの初心者です。レーキに疑問があります。create
関数は新しいデータベースを作成することです。その後、次のようなコマンドを実行します
rake db:load
rake db:data:load
rake db:schema:load
rake db:migrate
rake db:seed
しかし、dbを作成した後にこのcmdを実行する理由と、aboutcmdの機能について説明します。
アドバイスありがとうございます。
rake -T
各タスクの説明を取得するために使用できます。
$ rake -T | grep db
rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump # Dump the database structure to db/structure.sql. Specify another file with DB_STRUCTURE=db/my_structure.sql
rake db:version # Retrieves the current schema version number
私はあなたが何を求めていたのですか?
編集:
移行の詳細については、http://guides.rubyonrails.org/migrations.htmlを参照してください。
編集2:
rake db:migrate
DBスキーマを「適切な」方法で更新できます。新しい移行を作成し(ガイドを読んでください)、新しい列を追加できます。たとえば、インデックスを追加したり、列の名前を変更したりできます。 「時間」内を前後に移動します。移行を実行して、後でロールバックできます。
新しい移行を生成する場合:
$ rails g migration add_new_column_to_some_table
後で実行rake db:migrate
して、必要な変更に適用することができます(もちろん、この生成された移行の本体を作成する必要があります)。
私はあなたにガイドを読むことをお勧めします:)
編集3:
add_column :users, :price, :float
たとえば、テーブルにprice
列を追加します。列のタイプは次のようになります(お金に関連するものを保存するのは最善の方法ではありません!)。この列はデフォルトで表示されます。users
float
float
NULL
編集4:
実行された移行に関する情報はschema_migrations
テーブルに保存されます。初めて移行を実行するとversion
、この移行の新しいレコードがこのテーブルに追加されます(日付+ファイル名の乱数)。移行をロールバックすると、このレコードが削除されます。移行を2回実行しても、効果はありません。
簡単に言えば、db:migrateはデータベースにある既存のデータを破壊しません。したがって、移行を実行すると、そのrakeタスクにより、行った変更からデータが存在できるようになります。
create
は空のデータベースです。db/migrate
db/migrate
作成された移行ファイルを編集します。bundle exec rake db:migrate
です。これにより、2番目の移行ファイルの内容がデータベースに追加され、既存のデータが損なわれることはありません。bundle exec rake db:create:all
bundle exec rails generate scaffold Users name:string email:string
bundle exec rake db:migrate
bundle exec rails s
に移動しlocalhost:3000/users/new
て新しいユーザーを作成します。bundle exec rails generate scaffold Posts title:string body:text
bundle exec rake db:migrate
localhost:3000/users
と、作成したユーザーが表示されます。