Ruby on rails 如何在Ruby on Rails中使用.where NULL
我在一个表中有Ruby on rails 如何在Ruby on Rails中使用.where NULL,ruby-on-rails,ruby,Ruby On Rails,Ruby,我在一个表中有:parent\u id为空的项,表示它是一个主类别。我想知道如何使用.where(如果调用的方法正确的话)迭代这些 这是我的控制器: def new @title = 'New Post'; @post = Post.new @categories = BlogCategory.all @cat_zero = BlogCategory.where(:parent_id NULL) @cat_one = BlogCategory.where(parent_id:
:parent\u id
为空的项,表示它是一个主类别。我想知道如何使用.where
(如果调用的方法正确的话)迭代这些
这是我的控制器:
def new
@title = 'New Post';
@post = Post.new
@categories = BlogCategory.all
@cat_zero = BlogCategory.where(:parent_id NULL)
@cat_one = BlogCategory.where(parent_id: 1)
@cat_two = BlogCategory.where(parent_id: 2)
@cat_three = BlogCategory.where(parent_id: 3)
@cat_four = BlogCategory.where(parent_id: 4)
end
其余的工作正常,只是NULL不起作用。(BlogCategory和Post之间的关系已经确定。)
我试过做这些,但运气不佳(还有其他几个版本):
谢谢你的帮助 您要查找的拼写是:
@cat_zero = BlogCategory.where(parent_id: nil)
这与其他值的拼写相同,只使用了
nil
嘿,谢谢你这么做,但它引发了一个错误:语法错误,意外的关键字\u nil,期望''@cat\u zero=blogcontegory.where(:parent\u id nil)^
哇,尝试不同组合时犯了一个愚蠢的错误,我没意识到还是那样!谢谢你指出这一点。
@cat_zero = BlogCategory.where(parent_id: nil)