Posts
有很多Comments
。每个Comment
可选地属于User
。获取所有没有空字段的记录
我如何获得有1个用户的帖子1的所有评论?
Comments
content post_id user_id
------- ------- -------
There is no user for this post. 1
This is an example with a user. 1 123
Another example with a user. 1 8321
事情我已经尝试:
这不返回任何记录
@comments = Post.find(1).comments.where(:user_id != nil)
这些返回的每条记录:
@comments = Post.find(1).comments.where("user_id IS NOT NULL")
@comments = Post.find(1).comments.where("user_id <> 0")
当我打印出来的user_ids在页面上,它看起来就像零读写为“0”,但在数据库中仍为空。