如何使用ransack检查DateTime字段是否为空

时间:2018-04-24 18:35:46

标签: ruby-on-rails activerecord ransack

我已经尝试了查询

<%= f.select :ends_at_blank, [true, false], {:include_blank => "All"}, class: "show-tick" %>

但它会创建sql错误:

PG::InvalidDatetimeFormat: ERROR:  invalid input syntax for type timestamp: ""
LINE 1: ...nds_at" IS NOT NULL AND "activities"."ends_at" != '')) LIMIT...
                                                             ^
: SELECT  "activities".* FROM "activities" LEFT OUTER JOIN "users" ON "users"."id" = "activities"."user_id" WHERE ("users"."user_type" ILIKE '%WPD - SURV/MGR%' AND ("activities"."ends_at" IS NOT NULL AND "activities"."ends_at" != '')) LIMIT $1 OFFSET $2

因为它正在检查空白字符串以及nil - &#39;&#39;

1 个答案:

答案 0 :(得分:1)

答案是使用_null而不是_blank

<%= f.select :ends_at_null, [true, false], {:include_blank => "All"}, class: "show-tick"  %>