0

I have the following piece of code that's broken:

 //user_posts_id is an array of integer values @comment_notifications = Comment.where(["author_id != ? and post_id in ?", current_user.id, user_posts_id]) 

This is part of the error:

PG::SyntaxError: ERROR: syntax error at or near "1" LINE 1: ... "comments" WHERE (author_id != 8 and post_id in 1,4,8,9,2,... ^ : SELECT "comments".* FROM "comments" WHERE (author_id != 8 and post_id in 1,4,8,9,2,3)

It doesn't seem to work because of the second conditional for "posts_id" and "user_posts_id". Also the "!=" complicates things a little, I think. What would be the appropriate way to write this ActiveRecord query?

Appreciate any suggestions! Thanks.

2 Answers 2

2

The problem is that you are sending the list without parens.

 @comment_notifications = Comment.where(["author_id != ? and post_id in (?)", , current_user.id, user_posts_id]) 

If you look at the error it's telling you that the problem is near the "1", which is the first item in your list.

Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in Rails other way also :

@comment_notifications = Comment.where .not(author_id: current_user.id) .where(post_id: user_posts_id) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.