对于显示错误的参考列,其中不为null - rails

时间:2016-06-10 12:41:14

标签: ruby-on-rails postgresql ruby-on-rails-4

我想查询一个表,其中多个引用的列不为空。

我试过这样的:     JobTypePresetting.where('comment_text ==?',nil)     JobTypePresetting.where('comment_text_id ==?OR part_listing_id ==?',nil,nil)

即使是第一个也没有显示错误,如

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: integer == unknown
LINE 1: ...resettings"."template_id" = $2 AND (comment_text_id == NULL)
                                                           ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我如何查询,目前我正在使用

JobTypePresetting.where('comment_text_id is not null or part_listing_id is not null')

2 个答案:

答案 0 :(得分:0)

你需要使用单个(=)oprator。

JobTypePresetting.where('comment_text =?',nil)

希望这可能适合你。

答案 1 :(得分:0)

尝试使用Arel,更容易构建复杂的SQL查询。它们已成为3版本中的rails的一部分。 https://github.com/rails/arel

jtp = JobTypePresetting.arel_table
JobTypePresetting.where(jtp[:comment_text_id].not_eq(nil).or(jtp[: part_listing_id].not_eq(nil)))

希望有所帮助

相关问题