Hitting unknown error with "can't dup NilClass"


My system has been encounter this problem, and I couldn't find solution after debugging. My scenario is stated below:

class user    has_many :posts    has_many :comments end

class post   belongs_to :user   has_many :comments, :as => :commentable end

class comment   belongs_to :post   belongs_to :user end

For this case, I am trying to retrieve each post's comment together with submitter username and I write as below:

class PostsController<ApplicationController   def show       raise @post.comments.first.user.inspect   end end

While the first time I run this script, it works fine. But, after I hitting a refresh, it hits following problem "can't dup NilClass". It is repeats the same for everytime i restart mongrel, working for the first time and failed for the rest.

I append the application error log as below: /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2189:in `dup' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2189:in `scoped_methods' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2193:in `current_scoped_methods' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2183:in `scope' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:1548:in `find_every' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:1588:in `find_one' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:1574:in `find_from_ids' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:616:in `find' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ associations/belongs_to_association.rb:44:in `find_target' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ associations/association_proxy.rb:240:in `load_target' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ associations/association_proxy.rb:112:in `reload' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ associations.rb:1231:in `user'

Need some help for this. Thanks in advance.

I've such problem. First time it works fine. But next time it gets error "can't dup NilClass"

Found solution - Use "unloadable" for these classes - Can't dup NilClass… maybe try `unloadable` – Daniel X's Blog

Yeah, I'm getting the same thing for all associations on one of my classes. have you found a solution?