:class_name option for association does not work with symbols #2530

Closed
alexeymuranov opened this Issue Aug 14, 2011 · 2 comments

Projects

None yet

2 participants

@alexeymuranov

I do not know if this should be a feature request or a bug report.
Maybe adding a line to the documentation could help.

I used to use symbols for the :class_name option in associations, but have just found out that it does not always work.
I wonder if this is an expected behavior and why i should always use strings.
In the following situation

class Member < ActiveRecord::Base
  ...
  has_one :history, :class_name => :MemberHistory,
                    :dependent  => :destroy
  ...
end

i get an error when i try to destroy a Member:

NoMethodError: undefined method `split' for :MemberHistory:Symbol
    from /Volumes/Data/Users/alexey/.rvm/gems/ruby-1.9.2-p290@rails_3_1/gems/activesupport-3.1.0.rc5/lib/active_support/inflector/methods.rb:119:in `constantize'

If i use strings instead (:class_name => 'MemberHistory'), it works.

This is in Rails 3.1.0rc5.

@franckverrot

A similar issue has been rejected and closed 2 months ago, so I think you'll have to use String's there. :)

@alexeymuranov

Thanks for pointing this out.
I'll change to strings, but symbols here would look more natural to me (my understanding is that symbols are like static strings, which exist in limited number in an application, and class names are rarely generated dynamically).
I'll think about closing this, but mentioning this in the documentation would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment