I have a column that has either 0 or 1 in it. If the column has 0 in it I want to display it in a view.
In my controller I have something like this:
def read
@title = "Something"
@posts = Post.where('read' == 0)
end
For some reason it gets all the posts even when read has the value of 1. What could I be doing wrong?
The correct form is
Post.where(:read => 0)What your current form does is compare the string
'read'to the value0. If we enter this comparison into a Ruby console, we can see what’s happening:So, that comparison is returning
false. And if you simply typePost.where(false)in a console, you’ll see that it returns allPostrecords, which explains why you’re getting everyPostobject returned. In other words, the line, as you’ve typed it, is equivalent to typingPost.where(false)