Rails: How to Check One Attribute vs Another in Where

Viewed 31

I have a Comment model and I want to check one attribute of an instance to ensure they aren't equal.

Comment.where.not(user_id: :post_user_id).find_each do |order|
  # 
end 

This isn't working though. When I run this, I seem to get no results, when I should get one.

I also tried comment.post_user_id and self.post_user_id, but neither worked.

Thanks in advance!

Edit - This got closed for lack of detail.

I'm unsure what to add for details. The Comment model has both the post_user_id and user_id attributes.

1 Answers

I don't know from where do you get value of post_user_id, but this should totally work:

Comment.where.not(user_id: 10).find_each do |comment|
  #
end

so in your case:

post_user_id = 10
# or maybe from params?
post_user_id = params[:post_user_id]
Comment.where.not(user_id: post_user_id).find_each do |comment|
  puts comment.id
end
Related