On Dec 15, 5:58 pm, peterb
<peter.bonn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi there,
> Rails 2.0.1 stable, mySQL
>
> trying to access an object created through a function call :
>
> Model 1 :
> MyModel << ActiceRecord::Base
> ...
> def stats
> @stats ||= MyStats.new(self.id)
> end
> ...
>
> Model 2 :
> MyStats < MyModel
> ...
> def new(id)
> self.id = id
> self.all
> end
>
> def all
> @all ||= OtherModel.find_by_my_model_id(self.id)
> end
>
> ...
>
> View :
> <% for myModel in @list_of_myModels %>
> <%=debug(myModel.stats)%>
>
> throws "can''t dup Fixnum " error
Nothing to do with that ticket you found.
You''ve overriden the instance method new, not the class method new, so
when you do MyStats.new, you''re calling the original
ActiveRecord::Base initialize, which expects a hash of options as its
argument. It dups that hash and so you get your error.
Either way, don''t override new. Either override initialize (and
remember to call super) or use the after_initialize callback.
Fred
Also you should be overriding initialize, not new (or possibly using
the after_initialize callback).
, and either way you''ve got to call the superclass constructor, or bad
stuff will happen
Fred>
> likewise for :
> <%=debug(@list_of_myModels[0].stats)%>
>
> I led myself here :http://dev.rubyonrails.org/changeset/7399
> But I''m confused. This seems an old patch.
> any help appreciated.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---