1.Rails Appliction Medium Create New Rails App ...
2. Create Application Framework: rails demo-d mysql (create applications that demo, and use the mysql database as the default database), will generate a pile of documents at this time
3. Open the config / database.yml, modify one of the database-related configuration, ensure that its able to connect to database (my database named default demo, because at the development stage, so the database is named demo_development)
4. Now begin to create the database, instantRails own use phpmyadmin to create the database demo_development
5. Create a scaffold, the command: ruby script / generate scaffold User name: string pass: string age: integer, order the implementation of successful, will generate the corresponding MVC documents, and the first version of the database migration document 001_create_users.
6. The previous step operation is only to generate the corresponding documents and did not generate the actual table in the database, it is necessary to generate the actual table, need to use the rake for data migration, the command: rake db: migrate, the implementation of successful, check the database, will found to generate a users table (Note: the table name here are users, the User than in front of many a s, is a plural form), but also generates a table named schemainfo, this table is the database version of Record of the information, observed inside the data field is only one version and the version at this time the value of one.
7. Run ruby script / server, the server up and running, open the browser User list appears, and CRUD corresponding appropriate link, CRUD the successful implementation of this point, one of the most easy to create the db demo completed successfully.
8.'s Step demo to create the success of this step in the users table I would like to add two fields in the new phone and mobile, at this time are still required to use the database migration tools rake, run ruby script / generate migration add_phonemobile, successful will generate a second version of the database migration document: 002_add_phonemobile.rb, edit the document, amended as follows:
class Add_Phonemobile < ActiveRecord::Migration def self.up add_column :users, :phone, :string add_column :users, :mobile, :string end def self.down remove_column :users, :phone remove_column :users, :mobile end end
9. Again run rake db: migrate, the implementation is completed, the database will find a new addition of phone and mobile two fields. Schemainfo observation data, version value to 2.
10. Summarize: rake db: migrate data migration at the time, will go search in the database version value (DBVersion), and migrate with the current application directory Finally migrate files to do a database comparison (AppVersion), if AppVersion = = DBVersion, it will not do data migration operations, only AppVersion> DBVersion, will make the relocation operation, this point must bear in mind that before I would for this reason that the time to do the database migration, the Executive order is not wrong, However, the fields inside the database is not changed.
11. If you want to return to the previous version of the database, you can run the command: rake db: migrate version = x, where x represents the generation want to return to the version number.
12. After the database migration, the refreshed view, I found the initial fields are generated name: string pass: string age: integer, the three fields, and no new field manual will be required at this time the new modified by the field to go CRUD view above, I do not know have no way to automatically update the view.
Well, the demo on this as to, as my first ror Study Notes. After Writing ror will stick to my notes, looking forward to learn faster! !







