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

前端 未结 4 714
太阳男子
太阳男子 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:22

    What I do when I'm looking for records with a "not true" condition (eg, false or nil) is:

    Post.where(["(hidden IS NULL) OR (hidden = ?)", false])
    

提交回复
热议问题