diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 28 | ||||
-rw-r--r-- | railties/guides/source/initialization.textile | 40 | ||||
-rw-r--r-- | railties/guides/source/plugins.textile | 26 | ||||
-rw-r--r-- | railties/guides/source/routing.textile | 2 |
4 files changed, 48 insertions, 48 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index e18dbc9c42..73f3ebafbe 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -86,7 +86,7 @@ Using <tt>Model.find(primary_key)</tt>, you can retrieve the object correspondin <ruby> # Find the client with primary key (id) 10. client = Client.find(10) -=> #<Client id: 10, name: => "Ryan"> +=> #<Client id: 10, first_name: => "Ryan"> </ruby> SQL equivalent of the above is: @@ -103,7 +103,7 @@ h5. +first+ <ruby> client = Client.first -=> #<Client id: 1, name: => "Lifo"> +=> #<Client id: 1, first_name: => "Lifo"> </ruby> SQL equivalent of the above is: @@ -120,7 +120,7 @@ h5. +last+ <ruby> client = Client.last -=> #<Client id: 221, name: => "Russel"> +=> #<Client id: 221, first_name: => "Russel"> </ruby> SQL equivalent of the above is: @@ -140,7 +140,7 @@ h5. Using Multiple Primary Keys <ruby> # Find the clients with primary keys 1 and 10. client = Client.find(1, 10) # Or even Client.find([1, 10]) -=> [#<Client id: 1, name: => "Lifo">, #<Client id: 10, name: => "Ryan">] +=> [#<Client id: 1, first_name: => "Lifo">, #<Client id: 10, first_name: => "Ryan">] </ruby> SQL equivalent of the above is: @@ -227,7 +227,7 @@ h4. Pure String Conditions If you'd like to add conditions to your find, you could just specify them in there, just like +Client.where("orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2. -WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, +Client.where("name LIKE '%#{params[:name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array. +WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, +Client.where("first_name LIKE '%#{params[:first_name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array. h4. Array Conditions @@ -544,7 +544,7 @@ In order to use optimistic locking, the table needs to have a column called +loc c1 = Client.find(1) c2 = Client.find(1) -c1.name = "Michael" +c1.first_name = "Michael" c1.save c2.name = "should fail" @@ -773,21 +773,21 @@ Even though Active Record lets you specify conditions on the eager loaded associ h3. Dynamic Finders -For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your +Client+ model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+. +For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have also have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+. You can do +find_last_by_*+ methods too which will find the last record matching your argument. You can specify an exclamation point (<tt>!</tt>) on the end of the dynamic finders to get them to raise an +ActiveRecord::RecordNotFound+ error if they do not return any records, like +Client.find_by_name!("Ryan")+ -If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked("Ryan", true)+. +If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_first_name_and_locked("Ryan", true)+. -There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns +nil+. The SQL looks like this for +Client.find_or_create_by_name("Ryan")+: +There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_first_name(params[:first_name])+. Using this will firstly perform a find and then create if the find returns +nil+. The SQL looks like this for +Client.find_or_create_by_first_name("Ryan")+: <sql> -SELECT * FROM clients WHERE (clients.name = 'Ryan') LIMIT 1 +SELECT * FROM clients WHERE (clients.first_name = 'Ryan') LIMIT 1 BEGIN -INSERT INTO clients (name, updated_at, created_at, orders_count, locked) +INSERT INTO clients (first_name, updated_at, created_at, orders_count, locked) VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', 0, '0') COMMIT </sql> @@ -795,10 +795,10 @@ COMMIT +find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the arguments you passed in. For example: <ruby> -client = Client.find_or_initialize_by_name('Ryan') +client = Client.find_or_initialize_by_first_name('Ryan') </ruby> -will either assign an existing client object with the name "Ryan" to the client local variable, or initialize a new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it. +will either assign an existing client object with the name "Ryan" to the client local variable, or initialize a new object similar to calling +Client.new(:first_name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it. h3. Finding by SQL @@ -869,7 +869,7 @@ SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan') You can also use the +includes+ or +joins+ methods for this to do something a little more complex: <ruby> -Client.count.where("clients.first_name = 'Ryan' AND orders.status = 'received'").includes("orders") +Client.where("clients.first_name = 'Ryan' AND orders.status = 'received'").includes("orders").count </ruby> Which will execute: diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 58ae115ba7..bc3a2d0f0b 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -102,7 +102,7 @@ Now with Rails 3 we have a Gemfile which defines the basics our application need <ruby> source 'http://rubygems.org' - gem 'rails', '3.0.0.beta1' + gem 'rails', '3.0.0.beta4' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' @@ -132,33 +132,33 @@ Now with Rails 3 we have a Gemfile which defines the basics our application need Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This is until you run +bundle pack+. This command freezes all the gems required by your application into _vendor/cache_. The gems installed by default are: * abstract-1.0.0.gem -* actionmailer-3.0.0.beta1.gem -* actionpack-3.0.0.beta1.gem -* activemodel-3.0.0.beta1.gem -* activerecord-3.0.0.beta1.gem -* activeresource-3.0.0.beta1.gem -* activesupport-3.0.0.beta1.gem -* arel-0.3.3.gem +* actionmailer-3.0.0.beta4.gem +* actionpack-3.0.0.beta4.gem +* activemodel-3.0.0.beta4.gem +* activerecord-3.0.0.beta4.gem +* activeresource-3.0.0.beta4.gem +* activesupport-3.0.0.beta4.gem +* arel-0.4.0.gem * builder-2.1.2.gem -* bundler-0.9.14.gem +* bundler-0.9.26.gem * erubis-2.6.5.gem -* i18n-0.3.6.gem -* mail-2.1.5.3.gem -* memcache-client-1.8.1.gem +* i18n-0.4.1.gem +* mail-2.2.4.gem +* memcache-client-1.8.3.gem * mime-types-1.16.gem * polyglot-0.3.1.gem * rack-1.1.0.gem -* rack-mount-0.6.3.gem -* rack-test-0.5.3.gem -* rails-3.0.0.beta1.gem -* railties-3.0.0.beta1.gem +* rack-mount-0.6.4.gem +* rack-test-0.5.4.gem +* rails-3.0.0.beta4.gem +* railties-3.0.0.beta4.gem * rake-0.8.7.gem -* sqlite3-ruby-1.2.5.gem +* sqlite3-ruby-1.3.0.gem * text-format-1.0.0.gem * text-hyphen-1.0.0.gem -* thor-0.13.4.gem -* treetop-1.4.5.gem -* tzinfo-0.3.18.gem +* thor-0.13.6.gem +* treetop-1.4.8.gem +* tzinfo-0.3.22.gem TODO: Prettify when it becomes more stable. diff --git a/railties/guides/source/plugins.textile b/railties/guides/source/plugins.textile index fc7c1e2625..a12434a95b 100644 --- a/railties/guides/source/plugins.textile +++ b/railties/guides/source/plugins.textile @@ -205,7 +205,7 @@ def load_schema ActiveRecord::Base.establish_connection(config[db_adapter]) load(File.dirname(__FILE__) + "/schema.rb") - require File.dirname(__FILE__) + '/../rails/init.rb' + require File.dirname(__FILE__) + '/../rails/init' end </ruby> @@ -218,7 +218,7 @@ Once you have these files in place, you can write your first test to ensure that <ruby> # vendor/plugins/yaffle/test/yaffle_test.rb -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class YaffleTest < Test::Unit::TestCase load_schema @@ -284,7 +284,7 @@ In this example you will add a method to String named +to_squawk+. To begin, cr <ruby> # vendor/plugins/yaffle/test/core_ext_test.rb -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class CoreExtTest < Test::Unit::TestCase def test_to_squawk_prepends_the_word_squawk @@ -311,7 +311,7 @@ NoMethodError: undefined method `to_squawk' for "Hello World":String Great - now you are ready to start development. -Then in +lib/yaffle.rb+ require +lib/core_ext.rb+: +Then in +lib/yaffle.rb+ require +lib/core_ext+: <ruby> # vendor/plugins/yaffle/lib/yaffle.rb @@ -380,7 +380,7 @@ To begin, set up your files so that you have: * *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class ActsAsYaffleTest < Test::Unit::TestCase end @@ -436,7 +436,7 @@ To start out, write a failing test that shows the behavior you'd like: * *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class Hickwall < ActiveRecord::Base acts_as_yaffle @@ -489,7 +489,7 @@ To start out, write a failing test that shows the behavior you'd like: * *vendor/plugins/yaffle/test/acts_as_yaffle_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class Hickwall < ActiveRecord::Base acts_as_yaffle @@ -579,7 +579,7 @@ As always, start with a test: * *vendor/plugins/yaffle/test/woodpecker_test.rb:* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' class WoodpeckerTest < Test::Unit::TestCase load_schema @@ -633,7 +633,7 @@ You can test your plugin's controller as you would test any other controller: * *vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' require 'woodpeckers_controller' require 'action_controller/test_process' @@ -693,7 +693,7 @@ You can test your plugin's helper as you would test any other helper: * *vendor/plugins/yaffle/test/woodpeckers_helper_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' include WoodpeckersHelper class WoodpeckersHelperTest < Test::Unit::TestCase @@ -816,7 +816,7 @@ This section will describe how to create a simple generator that adds a file. F * *vendor/plugins/yaffle/test/definition_generator_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' require 'rails_generator' require 'rails_generator/scripts/generate' @@ -990,7 +990,7 @@ To start, add the following test method: * *vendor/plugins/yaffle/test/route_generator_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' require 'rails_generator' require 'rails_generator/scripts/generate' require 'rails_generator/scripts/destroy' @@ -1205,7 +1205,7 @@ This example will demonstrate how to use one of the built-in generator methods n * *vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb* <ruby> -require File.dirname(__FILE__) + '/test_helper.rb' +require File.dirname(__FILE__) + '/test_helper' require 'rails_generator' require 'rails_generator/scripts/generate' diff --git a/railties/guides/source/routing.textile b/railties/guides/source/routing.textile index cf733bd6f8..54af42a03f 100644 --- a/railties/guides/source/routing.textile +++ b/railties/guides/source/routing.textile @@ -204,7 +204,7 @@ In each of these cases, the named routes remain the same as if you did not use + |GET |photos/new |new | photos_path | |POST |photos |create | photos_path | |GET |photos/1 |show | photo_path(id) | -|GET |photos/1/edit |edit | dmin_photo_path(id) | +|GET |photos/1/edit |edit | edit_photo_path(id) | |PUT |photos/1 |update | photo_path(id) | |DELETE |photos/1 |destroy | photo_path(id) | |