very strange activerecord behavior

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

I meant that @question.class yielded "Question"

Hi James,

gcnovus wrote:

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?

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

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

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

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

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