-3

Hi

I have 6 simple and fundamental Problem in RoR migration and although I googled it and read many article about it and research it for a week , I didn't understand it. please help me for these problems :


1 - When I create a model (for example "rails generate model Football_League") , I see in db/migration a "create_football_leagues.rb" . I want know what is the rules of RoR for changing the name that I gave to my migration ? ( My migration's name was FootballLeague and ruby cretaed "create_football_leagues.rb")


2 - What is the rules of naming ruby for creating a class in that file ? ( Ruby created a "CreateFootballLeague" class in "create_football_leagues.rb")


3 - After I use rake db:migrate , although I had 3 migration, I just see below in cmd : enter image description here and I didn't any migration for another 3 migrations whereas sometimes ago when I run a migration with rake db:migrate, all my migrations are migrated.


4 - After I type rake db:migrate and after that Rake execute "create_football_leagues.rb" I go to mysql and I saw a table with "schema_migrations" name. and in that table there are some versions of my migrations. now I want to know what is the application of such a table ?


5 - In http://guides.rubyonrails.org I read that if we want to change the primary key of a table from default id to our own field we must use :primary_key option but I didn't understand how can I use it .If someone can give an example for this subject.


6 - What is the difference between this 2 commands : 1 - "rails generate migration footabll" 2 - rails generate model football. I knew that with "rails generate model football" we must some extra file in app/model but I don't understand function of those files.

My friend please help me to solve these fundamental problems . special thanks .

* My RoR version is 3.1.3 *

ainternet73
  • 117
  • 2
  • 8

2 Answers2

2
  1. If you generate a model, the migraton name is create_ + the pluralized class name. Note Your class name should not contain underscores.
  2. Same as the file name, but camel-cased as is Ruby convention, instead of underscored.
  3. Previous migrations had already been run.
  4. It keeps track of the schema version.
  5. See this question.
  6. One creates a migration. One creates a Rails model object, which includes a migration.
Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Can you explain 3,4,5,6 in more details ? – ainternet73 Jan 10 '12 at 17:08
  • @ainternet73 What is there to explain? (That's rhetorical; as the comments state, this is (a) too much for one question, (b) largely self-explanatory, and (c) covered in other tutorials. SO isn't a tutorial forum, really. – Dave Newton Jan 10 '12 at 17:16
1

For 3,4:

Say you create two migrations. Then run rake db:migrate. Rails now has created the two tables defined in your migrations. In addition it wrote the version numbers of those migrations in the table schema_migrations.

If now you add a third migration and run rake db:migrate again, Rails will see in schema_migrations which migrations had already run and skip those (otherwise you could lose the data in those tables!). So it only runs the new migration and again adds the version of this in schema_migrations.

If you would run rake db:migrate again, it now would do nothing.

For 5: For a beginner without thorough understanding of Rails you should avoid changing this setting.

For 6: - rails generate migration football This would only create the migration file. You normally use this command if you want to change an existing table (for example to add new columns) - rails generate model football This will generate a model and a migration to create the table belonging to this model. A model in Rails is the class that belongs to a specific table (simplified, there are other uses for models too). In your football model you will later have all the code that is directly related to the football table in your database. Here you define relationships and validations.

You should work through some tutorials very thoroughly to get a clear understanding of such concepts.

thorsten müller
  • 5,621
  • 1
  • 22
  • 30