# Can't dup Fixnum?

**URL:** <https://www.ruby-forum.com/t/cant-dup-fixnum/123270>\
**Category:** Rails\
**Created:** [December 15, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/cant-dup-fixnum/123270 "2007-12-15T18:59:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@peterb](https://www.ruby-forum.com/u/peterb)\
**Post date:** [December 15, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/cant-dup-fixnum/123270/1 "2007-12-15T18:59:17Z")

</div>

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](http://dev.rubyonrails.org/changeset/7399)  
But I’m confused. This seems an old patch.  
any help appreciated.

---

<div class="post-metadata">

**Author:** ![peterb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@peterb](https://www.ruby-forum.com/u/peterb)\
**Post date:** [December 15, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/cant-dup-fixnum/123270/2 "2007-12-15T21:46:29Z")

</div>

On Dec 15, 5:58 pm, peterb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
