Can't dup Fixnum?

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

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.

On Dec 15, 5:58 pm, peterb [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs