diff options
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 11 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 23 |
3 files changed, 22 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 40eeb481e5..1b25605727 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -193,10 +193,7 @@ module ActiveRecord # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects. # * <tt>collection.empty?</tt> - returns true if there are no associated objects. # * <tt>collection.size</tt> - returns the number of associated objects. - # * <tt>collection.find(id)</tt> - finds an associated object responding to the +id+ and that - # meets the condition that it has to be associated with this object. - # * <tt>collection.find_all(conditions = nil, orderings = nil, limit = nil, joins = nil)</tt> - finds all associated objects responding - # criteria mentioned (like in the standard find_all) and that meets the condition that it has to be associated with this object. + # * <tt>collection.find</tt> - finds an associated object according to the same rules as Base.find. # * <tt>collection.build(attributes = {})</tt> - returns a new object of the collection type that has been instantiated # with +attributes+ and linked to this object through a foreign key but has not yet been saved. *Note:* This only works if an # associated object already exists, not if its nil! @@ -205,14 +202,13 @@ module ActiveRecord # *Note:* This only works if an associated object already exists, not if its nil! # # Example: A Firm class declares <tt>has_many :clients</tt>, which will add: - # * <tt>Firm#clients</tt> (similar to <tt>Clients.find_all "firm_id = #{id}"</tt>) + # * <tt>Firm#clients</tt> (similar to <tt>Clients.find :all, :conditions => "firm_id = #{id}"</tt>) # * <tt>Firm#clients<<</tt> # * <tt>Firm#clients.delete</tt> # * <tt>Firm#clients.clear</tt> # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>) # * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>) - # * <tt>Firm#clients.find</tt> (similar to <tt>Client.find_on_conditions(id, "firm_id = #{id}")</tt>) - # * <tt>Firm#clients.find_all</tt> (similar to <tt>Client.find_all "firm_id = #{id}"</tt>) + # * <tt>Firm#clients.find</tt> (similar to <tt>Client.find(id, :conditions => "firm_id = #{id}")</tt>) # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new("firm_id" => id)</tt>) # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new("client_id" => id); c.save; c</tt>) # The declaration can also include an options hash to specialize the behavior of the association. diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 5e4906c3fb..a775f438f3 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -20,6 +20,7 @@ module ActiveRecord end end + # DEPRECATED. def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) if @options[:finder_sql] records = @association_class.find_by_sql(@finder_sql) @@ -31,6 +32,11 @@ module ActiveRecord end end + # DEPRECATED. Find the first associated record. All arguments are optional. + def find_first(conditions = nil, orderings = nil) + find_all(conditions, orderings, 1).first + end + # Count the number of associated records. All arguments are optional. def count(runtime_conditions = nil) if @options[:counter_sql] @@ -43,11 +49,6 @@ module ActiveRecord @association_class.count(sql) end end - - # Find the first associated record. All arguments are optional. - def find_first(conditions = nil, orderings = nil) - find_all(conditions, orderings, 1).first - end def find(*args) options = Base.send(:extract_options_from_args!, args) diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 6cca750644..3f0fa1c0b6 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -317,8 +317,8 @@ class HasManyAssociationsTest < Test::Unit::TestCase def test_find_all firm = Firm.find_first assert_equal firm.clients, firm.clients.find_all - assert_equal 2, firm.clients.find_all("type = 'Client'").length - assert_equal 1, firm.clients.find_all("name = 'Summit'").length + assert_equal 2, firm.clients.find(:all, :conditions => "type = 'Client'").length + assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length end def test_find_all_sanitized @@ -682,7 +682,7 @@ class BelongsToAssociationsTest < Test::Unit::TestCase def test_field_name_same_as_foreign_key computer = Computer.find 1 - assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" + assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # ' end def xtest_counter_cache @@ -705,12 +705,7 @@ end class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase - def setup - @accounts, @companies, @developers, @projects, @developers_projects = - create_fixtures "accounts", "companies", "developers", "projects", "developers_projects" - - @signals37 = Firm.find(1) - end + fixtures :accounts, :companies, :developers, :projects, :developers_projects def test_has_and_belongs_to_many david = Developer.find(1) @@ -914,4 +909,12 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase @active_record.developers.reload assert_equal @developers["david"].find, @active_record.developers.find(@developers["david"]["id"]), "Ruby find" end -end + + def xtest_find_in_association_with_options + developers = @active_record.developers.find(:all) + assert_equal 2, developers.size + + assert_equal @david, @active_record.developers.find(:first, :conditions => "salary < 10000") + assert_equal @jamis, @active_record.developers.find(:first, :order => "salary DESC") + end +end
\ No newline at end of file |