Reputation: 1
I'm a little new to rails, so pardon the noobish question.
I have a blog type application in rails using mongoid that has a users section and a comments section. (The comments are embedded within the article). So I was having trouble with the delete method of my comments controller. In order to delete them I traverse my collection, and look for where the id that the user clicked, the params[:id] equals the id in the database, the comment.id. Using print statements, I was able to find out that this does work and that the two id's should be equal. However, for some reason when I use the equals operator (==) in rails, it's registering the comment as nil.
Any help would be appreciated!
def destroy
@article = Article.find(params[:article_id])
logger.debug(@article)
@article.comments.each do |comment|
logger.debug(comment)
print comment.id.
print ", "
print params[:id]
print " | "
if comment.id.equal? params[:id]
comment.destroy unless comment.nil?
end
end
respond_to do |format|
format.html { redirect_to "/" }
format.js
end
end
Upvotes: 0
Views: 981
Reputation: 115511
comment.id
is a Bson object, params[:id]
is a string.
They match when you print because what is displayed is comment.id.to_s
Anyway you'd rather do:
@article.comments.where(:id => params[:id]).first
Or:
@article.comments.where(:_id => params[:id]).first
I'm not sure for Mongoid.
Upvotes: 1