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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I meant that @question.class yielded "Question" --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 8, 2007, at 11:27 PM, Bill Walton wrote:> 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, > BillYou 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Biedenharn wrote:> On Jul 8, 2007, at 11:27 PM, Bill Walton 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-- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Biedenharn wrote: > > On Jul 8, 2007, at 11:27 PM, Bill Walton 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 > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Biedenharn wrote: > > On Jul 8, 2007, at 11:27 PM, Bill Walton 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 > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jul 9, 9:31 pm, gcnovus <james.a.ro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > 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 Biedenharn wrote: > > > On Jul 8, 2007, at 11:27 PM, Bill Walton 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 > > > -- > > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---