From e99a0b62ec128272350060554a0f41673fcdb78d Mon Sep 17 00:00:00 2001 From: Jason Noble & Ralph Shnelvar Date: Sun, 24 Jul 2011 14:51:48 -0600 Subject: Add a note about the fact that the Windows prompt will look different than the linux/osx prompt --- railties/guides/source/getting_started.textile | 2 ++ 1 file changed, 2 insertions(+) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index aaad63523a..b3b3581147 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -217,6 +217,8 @@ If you follow this guide, you'll create a Rails project called blog, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed. +TIP: The examples below use # and $ to denote terminal prompts. If you are using Windows, your prompt will look something like c:\source_code> + h4. Installing Rails In most cases, the easiest way to install Rails is to take advantage of RubyGems: -- cgit v1.2.3 From 72e06dc7a01a999e786106ae410583642ff81e6d Mon Sep 17 00:00:00 2001 From: Jason Noble & Ralph Shnelvar Date: Sun, 24 Jul 2011 15:04:12 -0600 Subject: Fix TIP formatting issue --- railties/guides/source/getting_started.textile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index b3b3581147..94778608ae 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -249,7 +249,8 @@ $ rails new blog This will create a Rails application called Blog in a directory called blog. TIP: You can see all of the switches that the Rails application builder accepts -by running rails new -h. +by running +rails new -h. After you create the blog application, switch to its folder to continue work directly in that application: -- cgit v1.2.3 From 715c128e6a454addbb95df17dbc14cb3780bcea6 Mon Sep 17 00:00:00 2001 From: Jason Noble & Ralph Shnelvar Date: Sun, 24 Jul 2011 15:33:59 -0600 Subject: Remove "bundle install" section, as rails new ______ runs bundle install for you --- railties/guides/source/getting_started.textile | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 94778608ae..b01576b85c 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -282,18 +282,6 @@ by default: |tmp/|Temporary files| |vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.| -h4. Installing the Required Gems - -Rails applications manage gem dependencies with -"Bundler":http://gembundler.com/v1.0/index.html by default. As we don't need any -other gems beyond the ones in the generated +Gemfile+ we can directly run - - -$ bundle install - - -to have them ready. - h4. Configuring a Database Just about every Rails application will interact with a database. The database -- cgit v1.2.3 From fe5cb7c521d9cc452454fea4146af173cfe82c94 Mon Sep 17 00:00:00 2001 From: Jason Noble & Ralph Shnelvar Date: Sun, 24 Jul 2011 15:39:22 -0600 Subject: Update wording of TIP about --database option for rails new --- railties/guides/source/getting_started.textile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index b01576b85c..4c61cae0cb 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -397,12 +397,12 @@ development: Change the username and password in the +development+ section as appropriate. -TIP: You don't have to update the database configurations manually. If you had a -look at the options of application generator, you have seen that one of them is -named --database. It lets you choose an adapter for couple of most used -relational databases. You can even run the generator repeatedly: cd .. && -rails new blog --database=mysql. When you confirm the overwriting of the -+config/database.yml+ file, your application will be configured for MySQL +TIP: You don't have to update the database configurations manually. If you look at the +options of the application generator, you will see that one of the options +is named --database. This option allows you to choose an adapter from a +list of the most used relational databases. You can even run the generator +repeatedly: cd .. && rails new blog --database=mysql. When you confirm the overwriting + of the +config/database.yml+ file, your application will be configured for MySQL instead of SQLite. h4. Creating the Database -- cgit v1.2.3 From 2f7ea1907dbaae737a227977b9b645e4ae22c8a0 Mon Sep 17 00:00:00 2001 From: Jason Noble & Ralph Shnelvar Date: Sun, 24 Jul 2011 16:07:41 -0600 Subject: Make the rails not found tip more explanatory. --- railties/guides/source/getting_started.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 4c61cae0cb..c2c8ee02ad 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -462,8 +462,8 @@ your terminal: $ rails generate controller home index -TIP: If you're on Windows, or your Ruby is set up in some non-standard fashion, -you may need to explicitly pass Rails +rails+ commands to Ruby: ruby +TIP: If you get a command not found error when running this command, you +need to explicitly pass Rails +rails+ commands to Ruby: ruby \path\to\your\application\script\rails generate controller home index. Rails will create several files for you, including -- cgit v1.2.3 From 6734bc98e7a90dd7c4dce47750721a0d19170d83 Mon Sep 17 00:00:00 2001 From: Mike Howard Date: Sun, 24 Jul 2011 16:09:38 -0600 Subject: Updated migrations.textile to clarify Using Models in Your Migrations Rewrote entire section including example code, error message, and work around. --- railties/guides/source/migrations.textile | 103 ++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 18 deletions(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index c88e3cc338..e51ee0f535 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -477,7 +477,7 @@ Several methods are provided that allow you to control all this: For example, this migration - +
 class CreateProducts < ActiveRecord::Migration
   def change
     suppress_messages do
@@ -496,7 +496,7 @@ class CreateProducts < ActiveRecord::Migration
     end
   end
 end
-
+
generates the following output @@ -514,40 +514,107 @@ If you just want Active Record to shut up then running +rake db:migrate VERBOSE= h3. Using Models in Your Migrations -When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed. +When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done, but some caution should be observed. -Consider for example a migration that uses the +Product+ model to update a row in the corresponding table. Alice later updates the +Product+ model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with +rake db:migrate+, including the one that used the +Product+ model. When the migration runs the source is up to date and so the +Product+ model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist. +For example, problems occur when the model uses database columns which are (1) not currently in the database and (2) will be created by this or a subsequent migration. -Frequently I just want to update rows in the database without writing out the SQL by hand: I'm not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example: +Consider this example, where Alice and Bob are working on the same code base which contains a +Product+ model: - -class AddPartNumberToProducts < ActiveRecord::Migration - class Product < ActiveRecord::Base +Bob goes on vacation. + +Alice creates a migration for the +products+ table which adds a new column and initializes it. +She also adds a validation to the Product model for the new column. + +
+# db/migrate/20100513121110_add_flag_to_product.rb
+
+class AddFlagToProduct < ActiveRecord::Migration
+  def change
+    add_column :products, :flag, :int
+    Product.all.each { |f| f.update_attributes!(:flag => 'false') }
   end
+end
+
+ +
+# app/model/product.rb
+
+class Product < ActiveRecord::Base
+  validates_presence_of :flag
+end
+
+ +Alice adds a second migration which adds and initializes another column to the +products+ table and also adds a validation to the Product model for the new column. +
+# db/migrate/20100515121110_add_fuzz_to_product.rb
+
+class AddFuzzToProduct < ActiveRecord::Migration
   def change
-    ...
+    add_column :products, :fuzz, :string
+    Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' }
   end
 end
-
-The migration has its own minimal copy of the +Product+ model and no longer cares about the +Product+ model defined in the application.
+
-h4. Dealing with Changing Models +
+# app/model/product.rb
 
-For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try to use the old column information. You can force Active Record to re-read the column information with the +reset_column_information+ method, for example
+class Product < ActiveRecord::Base
+  validates_presence_of :flag
+  validates_presence_of :fuzz
+end
+
- -class AddPartNumberToProducts < ActiveRecord::Migration +Both migrations work for Alice. + +Bob comes back from vacation and: + +# updates the source - which contains both migrations and the latests version of the Product model. +# runs outstanding migrations with +rake db:migrate+, which includes the one that updates the +Product+ model. + +The migration crashes because when the model attempts to save, it tries to validate the second added column, which is not in the database when the _first_ migration runs. + +
+rake aborted!
+An error has occurred, this and all later migrations canceled:
+
+undefined method `fuzz' for #
+
+ +A fix for this is to create a local model within the migration. This keeps rails from running the validations, so that the migrations run to completion. + +When using a faux model, it's a good idea to call +Product.reset_column_information+ to refresh the ActiveRecord cache for the Product model prior to updating data in the database. + +If Alice had done this instead, there would have been no problem: + +
+# db/migrate/20100513121110_add_flag_to_product.rb
+
+class AddFlagToProduct < ActiveRecord::Migration
   class Product < ActiveRecord::Base
   end
+  def change
+    add_column :products, :flag, :int
+    Product.reset_column_information  
+    Product.all.each { |f| f.update_attributes!(:flag => false) }
+  end
+end
+
+
+# db/migrate/20100515121110_add_fuzz_to_product.rb
+
+class AddFuzzToProduct < ActiveRecord::Migration
+  class Product < ActiveRecord::Base
+  end
   def change
-    add_column :product, :part_number, :string
+    add_column :products, :fuzz, :string
     Product.reset_column_information
-    ...
+    Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' }
   end
 end
-
+
h3. Schema Dumping and You -- cgit v1.2.3