Skip to content
Browse files

Allow the :class_name option for associations to take a symbol.

This is to avoid confusing newbies, and to be consistent with the fact
that other options like :foreign_key already allow a symbol or a string.
  • Loading branch information...
1 parent 2c67845 commit e6cc2ea201857a5f3700f589336df2880564cfea @jonleighton jonleighton committed
View
8 activerecord/CHANGELOG
@@ -1,5 +1,13 @@
*Rails 3.2.0 (unreleased)*
+* Allow the :class_name option for associations to take a symbol (:Client) in addition to
+ a string ('Client').
+
+ This is to avoid confusing newbies, and to be consistent with the fact that other options
+ like :foreign_key already allow a symbol or a string.
+
+ [Jon Leighton]
+
* In development mode the db:drop task also drops the test database. For symmetry with
the db:create task. [Dmitriy Kiriyenko]
View
2 activerecord/lib/active_record/reflection.rb
@@ -124,7 +124,7 @@ def klass
# <tt>composed_of :balance, :class_name => 'Money'</tt> returns <tt>'Money'</tt>
# <tt>has_many :clients</tt> returns <tt>'Client'</tt>
def class_name
- @class_name ||= options[:class_name] || derive_class_name
+ @class_name ||= (options[:class_name] || derive_class_name).to_s
end
# Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +active_record+ attribute,
View
6 activerecord/test/cases/reflection_test.rb
@@ -189,7 +189,7 @@ def test_association_reflection_in_modules
def test_reflection_of_all_associations
# FIXME these assertions bust a lot
- assert_equal 36, Firm.reflect_on_all_associations.size
+ assert_equal 37, Firm.reflect_on_all_associations.size
assert_equal 26, Firm.reflect_on_all_associations(:has_many).size
assert_equal 10, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
@@ -325,6 +325,10 @@ def test_through_reflection_conditions_do_not_modify_other_reflections
assert_equal orig_conds, Post.reflect_on_association(:first_blue_tags_2).conditions.inspect
end
+ def test_symbol_for_class_name
+ assert_equal Client, Firm.reflect_on_association(:unsorted_clients_with_symbol).klass
+ end
+
private
def assert_reflection(klass, association, options)
assert reflection = klass.reflect_on_association(association)
View
1 activerecord/test/models/company.rb
@@ -42,6 +42,7 @@ class Firm < Company
:before_remove => :log_before_remove,
:after_remove => :log_after_remove
has_many :unsorted_clients, :class_name => "Client"
+ has_many :unsorted_clients_with_symbol, :class_name => :Client
has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false

0 comments on commit e6cc2ea

Please sign in to comment.
Something went wrong with that request. Please try again.