I'm using the acts_as_commentable_with_threading
plugin. It comes with a comment model in comment.rb
:
class Comment < ActiveRecord::Base
....
# NOTE: Comments belong to a user
belongs_to :user
....
However, when I have a Comment
object called comment
, I can't call comment.user
as I would have expected to be able to. In particular, I end up with this error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
and following the stack trace reveals that it occurs at this line:
def thumb_image_for(comment)
if comment.user.facebook_uid #### ERROR ON THIS LINE
...
I tried amending the definition to this:
#this method was called as thumb_image_for(comment.user)
def thumb_image_for(comment)
puts comment.to_yaml
puts "\n\nUser is: #{comment.user}\n\n"
puts "\n\nUser id is: #{comment.user_id}\n\n"
puts "\n\nUser yaml on find is: #{User.find(comment.user_id).to_yaml}\n\n"
puts "\n\nUser yaml is: #{comment.user.to_yaml}\n\n" #### ERROR ON THIS LINE
if comment.user.facebook_uid
...
And here's the output. As you can see, the user_id
is valid because I can look up the user and print out his data, I just can't get it straight from comment.user
. What's going on?
--- !ruby/object:Comment
attributes:
created_at: 2010-08-11 20:40:42
body: asdfasdfasdf
title: ""
commentable_type: Playlist
updated_at: 2010-08-11 20:40:42
flag:
commentable_id: 10
lft: 1
subject: ""
id: 102
user_id: 0
parent_id:
visitor_name:
rgt: 2
attributes_cache: {}
User is: #<User:0x105dbb540>
User id is: 0
User yaml on find is: --- !ruby/object:User
attributes:
name: user1281557087
last_request_at: 2010-08-11 21:03:49
created_at: 2010-08-11 20:04:47
facebook_uid: 1326120171
updated_at: 2010-08-11 21:03:49
moderation_reject_correct: 0
moderation_approve_level: 0
gender: 0
current_login_ip:
openid_identifier:
id: 0
moderation_approve_count: 0
current_login_at:
one_liner:
moderation_reject_level: 0
birthday:
facebook_session_key:
login_count: 0
locale: en_US
last_login_ip:
moderation_approve_correct: 0
email_autoset: f
last_login_at:
login:
moderation_reject_count: 0
email:
attributes_cache: {}
ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?) on line #5 of app/views/shared/_comment.html.haml:
2: =link_to "", "", :name => "#{comment_counter}"
3: .comment-info
4: -puts comment.to_yaml
5: = thumb_image_for_user_if_non_nil(comment)
6: -#%img.avatar{:alt => "", :height => "40", :src => "images/gravatar.jpg", :width => "40"}/
7: %cite
8: =link_to_user_if_non_nil(comment)
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/yaml.rb:389:in `quick_emit'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/yaml/rubytypes.rb:15:in `to_yaml'
app/helpers/playlists_helper.rb:33:in `thumb_image_for_user_if_non_nil'
app/views/shared/_comment.html.haml:5:in `_run_haml_app47views47shared47_comment46html46haml_locals_comment_comment_counter_comment_ids_object'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
haml (3.0.15) lib/haml/helpers.rb:90:in `non_haml'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
app/views/shared/_comments.html.haml:6:in `_run_haml_app47views47shared47_comments46html46haml_locals_comments_object'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
haml (3.0.15) lib/haml/helpers.rb:90:in `non_haml'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
app/views/shared/_playlist_detail.html.haml:44:in `_run_haml_app47views47shared47_playlist_detail46html46haml_locals_object_playlist_playlist_detail'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
haml (3.0.15) lib/haml/helpers.rb:90:in `non_haml'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:11:in `render'
app/views/playlists/show.html.haml:1:in `_run_haml_app47views47playlists47show46html46haml'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:13:in `render'
haml (3.0.15) rails/./lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/playlists_controller.rb:111:in `show'
haml (3.0.15) rails/./lib/sass/plugin/rack.rb:41:in `call'
haml (3.0.15) rails/./lib/sass/plugin/rack.rb:41:in `call'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:162:in `start'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:95:in `start'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:92:in `each'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:92:in `start'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:23:in `start'
/Users/keone/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/webrick/server.rb:82:in `start'
Rendered rescues/_trace (186.4ms)
Rendered rescues/_request_and_response (0.3ms)
Rendering rescues/layout (200)
Thanks.