# Very strange activerecord behavior

**URL:** <https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900>\
**Category:** Rails\
**Created:** [July 9, 2007, 4:53am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900 "2007-07-09T04:53:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 9, 2007, 4:53am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/1 "2007-07-09T04:53:12Z")

</div>

I have the following class:

class Question \< ActiveRecord::Base

# table has quiz\_id, text, created\_at, updated\_at

belongs\_to :quiz  
has\_many :options  
validates\_presence\_of :text  
validates\_length\_of :text, :within =\> 3…255  
def to\_s  
text  
end  
end

in my tests, everything works fine. In my view, I have  
\<%= @question %\>  
which yields “ActionView::TemplateError (undefined method `to\_s’ for  
#Question:0x32c8d84)”

Interestingly,  
\<%= @question.text %\> yields the appropriate text.  
\<%= @question.text %\> yields “Question”  
\<%= @question.inspect %\> yields values for the fields text,  
created\_at, and updated\_at.

@question.options and @question.quiz both yield the same error as  
@question.to\_s

Anybody have any idea why this is happening?

-G C Novus

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 9, 2007, 4:54am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/2 "2007-07-09T04:54:12Z")

</div>

I meant that @question.class yielded “Question”

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 9, 2007, 5:28am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/3 "2007-07-09T05:28:14Z")

</div>

Hi James,

gcnovus wrote:

> def to\_s  
> \<%= @question.text %\> yields the appropriate text.  
> \<%= @question.text %\> yields “Question”  
> \<%= @question.inspect %\> yields values for the fields text,  
> created\_at, and updated\_at.
> 
> @question.options and @question.quiz both yield the same error as  
> @question.to\_s
> 
> Anybody have any idea why this is happening?

My first guess would be that it’s because to\_s is valid on object  
attributes, not on a “complex” object. While you haven’t shown it, you  
imply that you’re trying to use it on a 'complex" object (e.g.,  
@question.to\_s). If @questions.object and @questions.quiz would also  
yield  
objects, rather than object attributes, then I’d start there.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 9, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/4 "2007-07-09T14:31:26Z")

</div>

On Jul 8, 2007, at 11:27 PM, Bill W. wrote:

> > has\_many :options  
> > #Question:0x32c8d84)"  
> > Anybody have any idea why this is happening?  
> > Bill  
> > You might also be wary of using ‘text’ as a column name since that is  
> > a MySql type too. The definition of your to\_s might work better as:

def to\_s  
self.text # or the new name you use to replace ‘text’  
end

Or:

def to\_s  
read\_attribute(:text) # or the new name  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 10, 2007, 3:28am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/5 "2007-07-10T03:28:16Z")

</div>

Except . . . to\_s is explicitly defined to return text. Changing that  
to return read\_attribute(:text) makes no difference. Nor does  
changing the database column to “q\_text” or something else that can’t  
be interpreted as a keyword.

-Gaius

On Jul 9, 11:27 am, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 10, 2007, 3:32am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/6 "2007-07-10T03:32:16Z")

</div>

As an added complication, let’s say I have two quizzes in the  
database, Quiz1 and Quiz2. If I load up Quiz1 first, and try to look  
at it’s question1, I get the method not defined error. If I then load  
up Quiz2 and try to find its question49, it doesn’t exist.

Now, here’s the really weird thing… if I flush the database and  
restart the server (which re-inserts the same defaults: Quiz1 and  
Quiz2), then load Quiz2 first, that one gets the error on question49,  
and question1 for quiz1 doesn’t exist!

-Gaius

On Jul 9, 11:27 am, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 9, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/7 "2007-07-09T17:27:50Z")

</div>

The implicit point is that \<%= _stuff_ %\>  
will evaluate;  
returned = eval “_stuff_”

and add this to the html by calling “returned.to\_s”  
as such;  
\<%= @question %\>  
will call the .to\_s method on @question.  
if @question.to\_s is not defined, you get an error.

The same error you’re getting.

Rob B. wrote:

> On Jul 8, 2007, at 11:27 PM, Bill W. wrote:

> def to\_s  
> self.text # or the new name you use to replace ‘text’  
> end
> 
> Or:
> 
> def to\_s  
> read\_attribute(:text) # or the new name  
> end

---

<div class="post-metadata">

**Author:** ![gcnovus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/779978/32.png) [@gcnovus](https://www.ruby-forum.com/u/gcnovus)\
**Post date:** [July 10, 2007, 4:27am UTC](https://www.ruby-forum.com/t/very-strange-activerecord-behavior/108900/8 "2007-07-10T04:27:22Z")

</div>

If anyone is interested, the solution is this:

the quizzes that were being autocreated on startup weren’t valid. Why  
they didn’t throw an exception I don’t know. Why they still loaded  
into memory I don’t know (except that validations probably aren’t run  
on retrieval). But why on Earth did this prevent the associated  
objects from loading properly??? I have absolutely no clue.

Chalk one up to esoteric error messages.

-Gaius
