From 948248a23d1e60dcb73af7db6d3eb7c70ccd4a72 Mon Sep 17 00:00:00 2001 From: Kulbir Saini Date: Fri, 24 Sep 2010 04:04:39 +0530 Subject: Updated guide to use ActiveRecord::Base.where instead of ActiveRecord::Base.find. Added `where` as a method to has_many and has_and_belongs_to_many collections. --- railties/guides/source/association_basics.textile | 87 +++++++++++++++-------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/railties/guides/source/association_basics.textile b/railties/guides/source/association_basics.textile index dbef9463a9..3bf1c6295d 100644 --- a/railties/guides/source/association_basics.textile +++ b/railties/guides/source/association_basics.textile @@ -30,7 +30,7 @@ Now, suppose we wanted to add a new order for an existing customer. We'd need to Or consider deleting a customer, and ensuring that all of its orders get deleted as well: -@orders = Order.find_all_by_customer_id(@customer.id) +@orders = Order.where(:customer_id => @customer.id) @orders.each do |order| order.destroy end @@ -550,7 +550,7 @@ build_customer create_customer -h6. _association_(force_reload = false) +h6(#belongs_to-association). _association_(force_reload = false) The association method returns the associated object, if any. If no associated object is found, it returns +nil+. @@ -560,7 +560,7 @@ The association method returns the associated object, if any. If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument. -h6. _association_=(associate) +h6(#belongs_to-association_equal). _association_=(associate) The association= method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value. @@ -637,7 +637,7 @@ class Order < ActiveRecord::Base end -h6. +:counter_cache+ +h6(#belongs_to-counter_cache). +:counter_cache+ The +:counter_cache+ option can be used to make finding the number of belonging objects more efficient. Consider these models: @@ -733,7 +733,7 @@ end NOTE: There's no need to use +:include+ for immediate associations - that is, if you have +Order belongs_to :customer+, then the customer is eager-loaded automatically when it's needed. -h6. +:polymorphic+ +h6(#belongs_to-polymorphic). +:polymorphic+ Passing +true+ to the +:polymorphic+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide. @@ -747,7 +747,7 @@ The +:select+ option lets you override the SQL +SELECT+ clause that is used to r TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results. -h6. +:touch+ +h6(#belongs_to-touch). +:touch+ If you set the +:touch+ option to +:true+, then the +updated_at+ or +updated_on+ timestamp on the associated object will be set to the current time whenever this object is saved or destroyed: @@ -817,7 +817,7 @@ build_account create_account -h6. association(force_reload = false) +h6(#has_one-association). association(force_reload = false) The association method returns the associated object, if any. If no associated object is found, it returns +nil+. @@ -827,7 +827,7 @@ The association method returns the associated object, if any. If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument. -h6. association=(associate) +h6(#has_one-association_equal). association=(associate) The association= method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value. @@ -1029,6 +1029,7 @@ When you declare a +has_many+ association, the declaring class automatically gai * collection.empty? * collection.size * collection.find(...) +* collection.where(:conditions) * collection.exists?(...) * collection.build(attributes = {}, ...) * collection.create(attributes = {}) @@ -1054,6 +1055,7 @@ orders.clear orders.empty? orders.size orders.find(...) +orders.where(:conditions) orders.exists?(...) orders.build(attributes = {}, ...) orders.create(attributes = {}) @@ -1083,10 +1085,10 @@ The collection.delete method removes one or more objects from @customer.orders.delete(@order1) -WARNING: Objects will be in addition destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+. +WARNING: Additionally, objects will be destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+. -h6(#has_many-collection_equal). collection=objects +h6(#has_many-collection-equal). collection=objects The collection= method makes the collection contain only the supplied objects, by adding and deleting as appropriate. @@ -1102,11 +1104,11 @@ h6(#has_many-collection_singular_ids_ids). collection_singular_ids= The collection_singular_ids= method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate. -h6(#has_many-collection_clear). collection.clear +h6(#has_many-collection-clear). collection.clear The collection.clear method removes every object from the collection. This destroys the associated objects if they are associated with +:dependent => :destroy+, deletes them directly from the database if +:dependent => :delete_all+, and otherwise sets their foreign keys to +NULL+. -h6. collection.empty? +h6(#has_many-collection-empty). collection.empty? The collection.empty? method returns +true+ if the collection does not contain any associated objects. @@ -1116,7 +1118,7 @@ The collection.empty? method returns +true+ if the collection <% end %> -h6. collection.size +h6(#has_many-collection-size). collection.size The collection.size method returns the number of objects in the collection. @@ -1124,7 +1126,7 @@ The collection.size method returns the number of objects in th @order_count = @customer.orders.size -h6. collection.find(...) +h6(#has_many-collection-find). collection.find(...) The collection.find method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+. @@ -1132,11 +1134,22 @@ The collection.find method finds objects within the collection @open_orders = @customer.orders.find(:all, :conditions => "open = 1") -h6. collection.exists?(...) +WARNING: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is depricated. Use collection.where instead when you need to pass conditions. + +h6(#has_many-collection-where). collection.where(:conditions) + +The collection.where method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. + + +@open_orders = @customer.orders.where(:open => 1) # No query yet +@open_order = @open_orders.first # Now the database will queried + + +h6(#has_many-collection-exists). collection.exists?(...) The collection.exists? method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+. -h6(#has_many_collection_build). collection.build(attributes = {}, ...) +h6(#has_many-collection-build). collection.build(attributes = {}, ...) The collection.build method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will _not_ yet be saved. @@ -1145,7 +1158,7 @@ The collection.build method returns one or more new objects of :order_number => "A12345") -h6. collection.create(attributes = {}) +h6(#has_many-collection-create). collection.create(attributes = {}) The collection.create method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object _will_ be saved (assuming that it passes any validations). @@ -1193,7 +1206,7 @@ h6(#has_many-as). +:as+ Setting the +:as+ option indicates that this is a polymorphic association, as discussed earlier in this guide. -h6. +:autosave+ +h6(#has_many-autosave). +:autosave+ If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. @@ -1439,11 +1452,12 @@ When you declare a +has_and_belongs_to_many+ association, the declaring class au * collection.empty? * collection.size * collection.find(...) +* collection.where(:conditions) * collection.exists?(...) * collection.build(attributes = {}) * collection.create(attributes = {}) -In all of these methods, collection is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and collection_singular is replaced with the singularized version of that symbol.. For example, given the declaration: +In all of these methods, collection is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and collection_singular is replaced with the singularized version of that symbol. For example, given the declaration: class Part < ActiveRecord::Base @@ -1464,6 +1478,7 @@ assemblies.clear assemblies.empty? assemblies.size assemblies.find(...) +assemblies.where(:conditions) assemblies.exists?(...) assemblies.build(attributes = {}, ...) assemblies.create(attributes = {}) @@ -1476,7 +1491,7 @@ If the join table for a +has_and_belongs_to_many+ association has additional col WARNING: The use of extra attributes on the join table in a +has_and_belongs_to_many+ association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a +has_many :through+ association instead of +has_and_belongs_to_many+. -h6. collection(force_reload = false) +h6(#has_and_belongs_to_many-collection). collection(force_reload = false) The collection method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array. @@ -1484,7 +1499,7 @@ The collection method returns an array of all of the associate @assemblies = @part.assemblies -h6. collection<<(object, ...) +h6(#has_and_belongs_to_many-collection-lt_lt). collection<<(object, ...) The collection<< method adds one or more objects to the collection by creating records in the join table. @@ -1494,7 +1509,7 @@ The collection<< method adds one or more objects to the collec NOTE: This method is aliased as collection.concat and collection.push. -h6. collection.delete(object, ...) +h6(#has_and_belongs_to_many-collection-delete). collection.delete(object, ...) The collection.delete method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects. @@ -1502,11 +1517,11 @@ The collection.delete method removes one or more objects from @part.assemblies.delete(@assembly1) -h6. collection=objects +h6(#has_and_belongs_to_many-collection-equal). collection=objects The collection= method makes the collection contain only the supplied objects, by adding and deleting as appropriate. -h6. collection_singular_ids +h6(#has_and_belongs_to_many-collection_singular). collection_singular_ids The collection_singular_ids method returns an array of the ids of the objects in the collection. @@ -1514,11 +1529,11 @@ The collection_singular_ids method returns an array of the ids @assembly_ids = @part.assembly_ids -h6. collection_singular_ids=ids +h6(#has_and_belongs_to_many-collection_singular_ids_ids). collection_singular_ids=ids The collection_singular_ids= method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate. -h6. collection.clear +h6(#has_and_belongs_to_many-collection-clear). collection.clear The collection.clear method removes every object from the collection by deleting the rows from the joining table. This does not destroy the associated objects. @@ -1549,6 +1564,16 @@ The collection.find method finds objects within the collection :conditions => ["created_at > ?", 2.days.ago]) +WARNING: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is depricated. Use collection.where instead when you need to pass conditions. + +h6(#has_and_belongs_to_many-collection-where). collection.where(:conditions) + +The collection.where method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. It also adds the additional condition that the object must be in the collection. + + +@new_assemblies = @part.assemblies.where("created_at > ?", 2.days.ago) + + h6(#has_and_belongs_to_many-collection-exists). collection.exists?(...) The collection.exists? method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+. @@ -1605,7 +1630,7 @@ The +has_and_belongs_to_many+ association supports these options: * +:uniq+ * +:validate+ -h6. +:association_foreign_key+ +h6(#has_and_belongs_to_many-association_foreign_key). +:association_foreign_key+ By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix +_id+ added. The +:association_foreign_key+ option lets you set the name of the foreign key directly: @@ -1661,7 +1686,7 @@ Normally Rails automatically generates the proper SQL to count the association m NOTE: If you specify +:finder_sql+ but not +:counter_sql+, then the counter SQL will be generated by substituting +SELECT COUNT(*) FROM+ for the +SELECT ... FROM+ clause of your +:finder_sql+ statement. -h6. +:delete_sql+ +h6(#has_and_belongs_to_many-delete_sql). +:delete_sql+ Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the +:delete_sql+ option, you can specify a complete SQL statement to delete them yourself. @@ -1699,11 +1724,11 @@ h6(#has_and_belongs_to_many-include). +:include+ You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used. -h6. +:insert_sql+ +h6(#has_and_belongs_to_many-insert_sql). +:insert_sql+ Normally Rails automatically generates the proper SQL to create links between the associated classes. With the +:insert_sql+ option, you can specify a complete SQL statement to insert them yourself. -h6. +:join_table+ +h6(#has_and_belongs_to_many-join_table). +:join_table+ If the default name of the join table, based on lexical ordering, is not what you want, you can use the +:join_table+ option to override the default. @@ -1821,7 +1846,7 @@ If you have an extension that should be shared by many associations, you can use module FindRecentExtension def find_recent - find(:all, :conditions => ["created_at > ?", 5.days.ago]) + where("created_at > ?", 5.days.ago) end end -- cgit v1.2.3