Is there a way to invert an ActiveRecord::Relation query?

前端 未结 4 712
太阳男子
太阳男子 2021-02-08 03:31

Let\'s say we have the following:

irb> Post.where(:hidden => true).to_sql
=> \"SELECT `posts`.* FROM `posts` WHERE posts.hidden = 1\"

4条回答
  •  迷失自我
    2021-02-08 04:24

    With a different syntax, yes. Example:

    posts = Post.scoped.table # or Arel::Table.new("posts")
    posts.where(posts[:hidden].eq(true).not).to_sql
    # => SELECT  FROM `posts` WHERE NOT ((`posts`.`hidden` = 1))
    

提交回复
热议问题