Ruby 仅获取Rails中列=0的数据
我有一个列,其中包含0或1。如果列中有0,我希望在视图中显示它 在我的控制器中,我有如下内容:Ruby 仅获取Rails中列=0的数据,ruby,ruby-on-rails-3,activerecord,Ruby,Ruby On Rails 3,Activerecord,我有一个列,其中包含0或1。如果列中有0,我希望在视图中显示它 在我的控制器中,我有如下内容: def read @title = "Something" @posts = Post.where('read' == 0) end @posts = Post.where(:read => 0) 出于某种原因,即使read的值为1,它也会获得所有帖子。我可能做错了什么?您使用了错误的格式。您获取帖子的呼叫应如下所示: def read @title = "Som
def read
@title = "Something"
@posts = Post.where('read' == 0)
end
@posts = Post.where(:read => 0)
出于某种原因,即使read的值为1,它也会获得所有帖子。我可能做错了什么?您使用了错误的格式。您获取帖子的呼叫应如下所示:
def read
@title = "Something"
@posts = Post.where('read' == 0)
end
@posts = Post.where(:read => 0)
尝试阅读并查看
条件部分。如果要在哈希表示法中指定,则必须像在Ruby中构造哈希一样使用代码。正确的形式是Post。其中(:read=>0)
当前表单所做的是将字符串'read'
与值0
进行比较。如果我们在Ruby控制台中输入这个比较,我们可以看到发生了什么:
> 'read' == 0
=> false
因此,该比较返回false
。如果您在控制台中简单地键入Post.where(false)
,您将看到它返回所有Post
记录,这就解释了为什么要返回每个Post
对象。换句话说,您键入的行相当于键入Post.where(false)