Forum: Ruby on Rails AssociationTypeMismatch got Fixnum

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A458b4ef31f03963bf0e1a5ae627ff2c?d=identicon&s=25 An (Guest)
on 2009-02-02 11:50
(Received via mailing list)
I'm using rails 2.0.5 and mongrel.

I have a Page and a Source models.

class Source < ActiveRecord::Base
  has_many :pages
end

class Page < ActiveRecord::Base
  belongs_to :source
end

Basically I'm listing pages with their sources; In production and
development ( I use mongrel_cluster in the first and a single mongrel
on the second) I got this error:

ActiveRecord::AssociationTypeMismatch (Source expected, got Fixnum):
    /vendor/rails/activerecord/lib/active_record/associations/
association_proxy.rb:150:in `raise_on_type_mismatch'
    /vendor/rails/activerecord/lib/active_record/associations/
belongs_to_association.rb:22:in `replace'
[...]

The fact that scares me  *a lot* is that I get it on random basis and
reloading the page solves it.
It looks like that sometimes the record isn't retrieved correctly from
the db but instead it got a fixnum.


The fact that is a random behaviour doesn't give me any clue on how to
solve it.
Any idea?
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-02 12:12
(Received via mailing list)
what exaclty means randomly? does it happen in different places? is
your own code mentioned in the trace? wich call would fail?

it seems your code is expection an instance of Source but is getting a
number (maybe the id) instead.

in order to find the piece of code that is causing the error, you
gotta try reproducing it.
A458b4ef31f03963bf0e1a5ae627ff2c?d=identicon&s=25 An (Guest)
on 2009-02-02 12:51
(Received via mailing list)
Randomly means that usually the page load without errors, but
somethimes the same page throws the error above.
Refreshing the page fixes the problem.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-02 13:30
(Received via mailing list)
On 2 Feb 2009, at 10:49, An wrote:
>
I would at least start by following that stack trace a little further.

Fred
0fe7e6e8b40ccd34ff0335df055b40bb?d=identicon&s=25 Dhanraj p. (dhanraj_p)
on 2012-08-22 11:46
MaD wrote in post #776440:
> what exaclty means randomly? does it happen in different places? is
> your own code mentioned in the trace? wich call would fail?
>
> it seems your code is expection an instance of Source but is getting a
> number (maybe the id) instead.
>
> in order to find the piece of code that is causing the error, you
> gotta try reproducing it.

>>>>>>>>>>>>>ans
if you will remove (belongs_to :model name) then it will work
This topic is locked and can not be replied to.