Ruby或Rails:PG数据查询错误,

时间:2014-02-11 20:43:32

标签: ruby-on-rails postgresql type-conversion rails-activerecord

我正在尝试根据用户输入的字符串更新一些记录 - 它不会让我产生此错误:

PGError: ERROR:  invalid input syntax for type boolean: "ian"
LINE 1: UPDATE "subjects" SET treatment_group = 'ian' AND pref_rand ...

我从rails控制台中仔细检查了类型:

Subject.column_hash["treatment_group"].type
=> :string

输入法是:

Group Name: <%= text_field_tag(:treatment_group_name) %>

最后,在控制器中我有这个逻辑:

@group_to_randomize = Subject.where("study_site = ? AND treatment_group is null", params[:site_input].to_i).order("created_at ASC").limit(params[:group_size_input].to_i)

      flash[:notice] = "We are at the database level. #{@group_to_randomize.inspect}"
      if params[:treatment_group_name] != '' and params[:site_input] != '' and params[:group_size_input] != ''
        @group_to_randomize.update_all(["treatment_group = ? AND pref_rand = ?", params[:treatment_group_name, @treatment.to_i])
        flash[:notice] = "Subjects randomized, and assigned to group #{params[:treatment_group_name]}"
      else
        flash[:notice] = "Nothing saved, please fill in the form completely."
      end

因此更容易阅读,错误源于此行:

@group_to_randomize.update_all(["treatment_group = ? AND pref_rand = ?", params[:treatment_group_name], @treatment.to_i])

所以我无法弄清楚为什么它认为输入是一个布尔值,为什么它不会保存为字符串无论如何。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

由于语法错误,请修改此行

@group_to_randomize.update_all(treatment_group:params[:treatment_group_name], pref_rand: @treatment.to_i)