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

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

class comment
belongs_to :post
belongs_to :user

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

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:
base.rb:2189:in dup' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:2189:inscoped_methods’
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:inscope’
base.rb:1548:in find_every' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/ base.rb:1588:infind_one’
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:infind’
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:inload_target’
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:inuser’

Need some help for this. Thanks in advance.

Found solution - Use “unloadable” for these classes -

I’ve such problem. First time it works fine. But next time it gets
error “can’t dup NilClass”

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs