Got Fixnum problem

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.

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.

Hey Champs,
Thanks for the reply… :slight_smile:
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:inreplace’
/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:insend’
/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:ineach’
/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:ininitialize’
/scratch/Hemant/Sandboxes/checkrates/app/controllers/categories_controller.rb:19:in
new' /scratch/Hemant/Sandboxes/checkrates/app/controllers/categories_controller.rb:19:increate’


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…

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.