# Got Fixnum problem

**URL:** <https://www.ruby-forum.com/t/got-fixnum-problem/179405>\
**Category:** Rails\
**Created:** [November 24, 2009, 9:33am UTC](https://www.ruby-forum.com/t/got-fixnum-problem/179405 "2009-11-24T09:33:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [November 24, 2009, 9:33am UTC](https://www.ruby-forum.com/t/got-fixnum-problem/179405/1 "2009-11-24T09:33:18Z")

</div>

Hello Champs,

Actually thing is that i am executing a query to find records from  
database using find\_by\_name … Lets suppose its name is name only …  
So it is :-  
result = MODEL.find\_by\_name(params[:name]) … correct … I can get id of  
result also from here… from “result.id”

But now i want to store this id to be stored in my database w.r.t. an  
integer…  
So i executed as:-  
MODEL.new(:result =\> result.id).save … In db migrations i declared this  
result column as “t.integer :parent” … But still this is not working …  
Can anybody directs me to the right direction.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [November 24, 2009, 1:34pm UTC](https://www.ruby-forum.com/t/got-fixnum-problem/179405/2 "2009-11-24T13:34:11Z")

</div>

Hemant B. wrote:

> MODEL.new(:result =\> result.id).save … In db migrations i declared this  
> result column as “t.integer :parent” … But still this is not working …  
> Can anybody directs me to the right direction.

More error/debug information would be helpful…

Do you expect “MODEL.new(:result =\> 42).save” to actually work?

What do you have coded in your new method for that model? What are the  
validations in place, if any, that might be failing.

Because right now, you’re just passing an int to the model constructor.  
I’m not sure if you have a single model for both MODEL examples, or if  
these are different models.

Should it be MODEL.new(:parent =\> result.id).save ??? I don’t know,  
this is all guessing at this point.

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [November 25, 2009, 6:47am UTC](https://www.ruby-forum.com/t/got-fixnum-problem/179405/3 "2009-11-25T06:47:16Z")

</div>

Hey Champs,  
Thanks for the reply… 🙂  
This is the error…

ActiveRecord::AssociationTypeMismatch in CategoriesController#create  
Category(#-606967528) expected, got Fixnum(#-604046028)  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/associations/association\_proxy.rb:263:in  
`raise_on_type_mismatch' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations/belongs_to_association.rb:22:in`replace’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/associations.rb:1258:in  
`parent=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2745:in`send’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/base.rb:2745:in  
`attributes=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2741:in`each’  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/base.rb:2741:in  
`attributes=' /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2439:in`initialize’  
/scratch/Hemant/Sandboxes/checkrates/app/controllers/categories\_controller.rb:19:in  
`new' /scratch/Hemant/Sandboxes/checkrates/app/controllers/categories_controller.rb:19:in`create’

…  
Again telling that i had created a field named parent in db/migrations  
like this…  
t.integer :parent  
And assigning a value to it as:-  
:parent =\> Model.find(:first, :conditions =\> { :abc =\> params[:def] }  
).id

So now what you say champs … ? Where i am wrong …

Ar Chron wrote:

> Hemant B. wrote:
> 
> > MODEL.new(:result =\> result.id).save … In db migrations i declared this  
> > result column as “t.integer :parent” … But still this is not working …  
> > Can anybody directs me to the right direction.
> 
> More error/debug information would be helpful…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [November 25, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/got-fixnum-problem/179405/4 "2009-11-25T15:48:02Z")

</div>

Hemant B. wrote:

> ActiveRecord::AssociationTypeMismatch in CategoriesController#create  
> Category(#-606967528) expected, got Fixnum(#-604046028)

Can’t get much clearer than that… it doesn’t want a Fixnum.

> Again telling that i had created a field named parent in db/migrations  
> like this…  
> t.integer :parent

That’s all well and good database-wise, but what is the context around  
this statement:

> And assigning a value to it as:-  
> :parent =\> Model.find(:first, :conditions =\> { :abc =\> params[:def] }  
> ).id

ActiveRecord wants an instance of the Category model. What models are  
you working with and how are they defined? What is the Categories  
controller ‘create’ method doing? Posting more code and giving better  
context will yield beter answers and recommendations.
