【问题标题】:Select from DB without some value从没有任何价值的数据库中选择
【发布时间】:2016-08-26 10:13:53
【问题描述】:

我有自己的 rails 应用程序,我需要指定我想从 DB (pg) 获得什么。普通的,我们这样做:@posts = Post.all 我会在我的帖子表中获得所有帖子。

在帖子表中,我有一个类别,作为布尔值:(照片、视频等),当我只想选择回复照片类别的帖子时,我们这样做:@posts = Post.where(photo: true)

但我需要的是选择所有没有视频的帖子。它必须看起来像这样:@posts = Post.all.without(video:true)。如何实现?

更新

是否有可能阻止多个值?我的意思是类似Post.where.not(id: 1 && 2)。我真的不知道它应该是什么样子。我需要的是选择所有没有 ID 为 1 和 2 的帖子。

【问题讨论】:

    标签: ruby-on-rails ruby database postgresql


    【解决方案1】:

    试试

    @posts = Post.where.not(video: true)
    

    【讨论】:

      【解决方案2】:

      你可以使用.not,例如

      @posts = Post.where.not(video:true)
      

      【讨论】:

      • 或@posts = Post.where(video: false)
      【解决方案3】:

      您可以传递一个值数组:Post.where.not(id: [1, 2])

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-10
        相关资源
        最近更新 更多