diff options
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/association_basics.textile | 36 | ||||
-rw-r--r-- | railties/guides/source/getting_started.textile | 16 | ||||
-rw-r--r-- | railties/guides/source/i18n.textile | 2 |
3 files changed, 44 insertions, 10 deletions
diff --git a/railties/guides/source/association_basics.textile b/railties/guides/source/association_basics.textile index c69f2ae8c9..b1ee4b8be4 100644 --- a/railties/guides/source/association_basics.textile +++ b/railties/guides/source/association_basics.textile @@ -1371,7 +1371,41 @@ The +:through+ option specifies a join model through which to perform the query. h6(#has_many-uniq). +:uniq+ -Specify the +:uniq => true+ option to remove duplicates from the collection. This is most useful in conjunction with the +:through+ option. +Set the +:uniq+ option to true to keep the collection free of duplicates. This is mostly useful together with the +:through+ option. + +<ruby> +class Person < ActiveRecord::Base + has_many :readings + has_many :posts, :through => :readings +end + +person = Person.create(:name => 'john') +post = Post.create(:name => 'a1') +person.posts << post +person.posts << post +person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">] +Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>] +</ruby> + +In the above case there are two readings and +person.posts+ brings out both of them even though these records are pointing to the same post. + +Now let's set +:uniq+ to true: + +<ruby> +class Person + has_many :readings + has_many :posts, :through => :readings, :uniq => true +end + +person = Person.create(:name => 'honda') +post = Post.create(:name => 'a1') +person.posts << post +person.posts << post +person.posts.inspect # => [#<Post id: 7, name: "a1">] +Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>] +</ruby> + +In the above case there are still two readings. However +person.posts+ shows only one post because the collection loads only unique records. h6(#has_many-validate). +:validate+ diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index f547f29087..12f2bb146b 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -322,16 +322,15 @@ $ rm public/index.html We need to do this as Rails will deliver any static file in the +public+ directory in preference to any dynamic contact we generate from the controllers. -Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. There are only comments in this file, so we need to add at the top the following: +Now, you have to tell Rails where your actual home page is located. Open the file +config/routes.rb+ in your editor. This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with +:root to+, uncomment it and change it like the following: <ruby> -Blog::Application.routes.draw do |map| +Blog::Application.routes.draw do - root :to => "home#index" - - # The priority is based upon order of creation: - # first created -> highest priority. #... + # You can have the root of your site routed with "root" + # just remember to delete public/index.html. + root :to => "home#index" </ruby> The +root :to => "home#index"+ tells Rails to map the root action to the home controller's index action. @@ -475,7 +474,7 @@ $ rails console After the console loads, you can use it to work with your application's models: <shell> ->> p = Post.create(:content => "A new post") +>> p = Post.new(:content => "A new post") => #<Post id: nil, name: nil, title: nil, content: "A new post", created_at: nil, updated_at: nil> @@ -1194,7 +1193,7 @@ The +destroy+ action will find the post we are looking at, locate the comment wi h4. Deleting Associated Objects -If you delete a post then it's associated comments will also need to be deleted. Otherwise they would simply occupy space in the database. Rails allows you to use the +dependent+ option of an association to achieve this. Modify the Post model, +app/models/post.rb+, as follows: +If you delete a post then its associated comments will also need to be deleted. Otherwise they would simply occupy space in the database. Rails allows you to use the +dependent+ option of an association to achieve this. Modify the Post model, +app/models/post.rb+, as follows: <ruby> class Post < ActiveRecord::Base @@ -1486,6 +1485,7 @@ h3. Changelog "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2 +* July 12, 2010: Fixes, editing and updating of code samples by "Jaime Iniesta":http://jaimeiniesta.com * May 16, 2010: Added a section on configuration gotchas to address common encoding problems that people might have by "Yehuda Katz":http://www.yehudakatz.com * April 30, 2010: Fixes, editing and updating of code samples by "Rohit Arondekar":http://rohitarondekar.com * April 25, 2010: Couple of more minor fixups "Mikel Lindsaar":credits.html#raasdnil diff --git a/railties/guides/source/i18n.textile b/railties/guides/source/i18n.textile index b09bb470ee..63d22db485 100644 --- a/railties/guides/source/i18n.textile +++ b/railties/guides/source/i18n.textile @@ -287,7 +287,7 @@ You most probably have something like this in one of your applications: <ruby> # config/routes.rb -Yourapp::Application.routes.draw do |map| +Yourapp::Application.routes.draw do root :to => "home#index" end |