# Does :class\_name for belongs\_to have to be a string and why?

**URL:** <https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701>\
**Category:** Rails\
**Created:** [December 26, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701 "2009-12-26T18:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xanothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ed655f/32.png) [@Xanothos](https://www.ruby-forum.com/u/Xanothos)\
**Post date:** [December 26, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701/1 "2009-12-26T18:25:13Z")

</div>

Seemingly, when I use a symbol, I get an ActiveRecord error stating  
‘Can’t convert symbol into String.’ Why can’t I use a symbol here?  
Possibly this is a deeper misunderstanding of symbols in general on my  
part.

#ActiveRecord error  
class Book \< ActiveRecord::Base  
belongs\_to :author, :class\_name =\> :Person, :foreign\_key  
=\> :author\_id  
end

#works  
class Book \< ActiveRecord::Base  
belongs\_to :author, :class\_name =\> “Person”, :foreign\_key  
=\> :author\_id  
end

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Xanothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ed655f/32.png) [@Xanothos](https://www.ruby-forum.com/u/Xanothos)\
**Post date:** [December 26, 2009, 6:34pm UTC](https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701/2 "2009-12-26T18:34:35Z")

</div>

On Dec 26, 3:46 pm, Xanothos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Seemingly, when I use a symbol, I get an ActiveRecord error stating  
> ‘Can’t convert symbol into String.’ Why can’t I use a symbol here?  
> Possibly this is a deeper misunderstanding of symbols in general on my  
> part.

Well symbols and strings are not interchangeable in general (and  
glancing briefly at the source, activerecord calls various methods  
only available on strings on the :class\_name option).

Fred

> =\> :author\_id  
> end

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Xanothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ed655f/32.png) [@Xanothos](https://www.ruby-forum.com/u/Xanothos)\
**Post date:** [December 26, 2009, 11:47pm UTC](https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701/3 "2009-12-26T23:47:52Z")

</div>

… and you capitalised the symbol for “:Person”.

---

<div class="post-metadata">

**Author:** ![Xanothos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ed655f/32.png) [@Xanothos](https://www.ruby-forum.com/u/Xanothos)\
**Post date:** [December 27, 2009, 6:09am UTC](https://www.ruby-forum.com/t/does-class-name-for-belongs-to-have-to-be-a-string-and-why/180701/4 "2009-12-27T06:09:17Z")

</div>

Shouldn’t the symbol be capitalized _if_ it worked in this scenario?  
The class name is capitalized, ie: class name = Person therefore: p =  
Person.new. Why would the symbol be lowercase?

On Dec 26, 5:47 pm, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## … and you capitalised the symbol for “:Person”.
> 
> Posted viahttp://www.ruby-forum.com/.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
