@preComments = Comment.where(:resource_hash => resource_hash).sort(:created_at.desc).all
@preComments.each do |comment|
u = ::User.find_by_id comment.user_id
p u
@comments << @preComments
p "HERE!!!!!"
end
That’s my code, but @comments isn’t defined so I get an 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.<<
If I create an array first, then my view can’t read it. So how do I do this?
The problem is that the first time you iterate, you want to create the @comments array (containing that item), but all subsequent times you want to push that item onto the existing array. There’s probably a more elegant way to do this, but I generally just do this: