【问题标题】:PG::Error: ERROR: argument of WHERE must be type boolean, not type integerPG::Error: 错误:WHERE 的参数必须是布尔类型,而不是整数类型
【发布时间】:2013-10-15 20:07:02
【问题描述】:

我收到了这个 heroku 错误,

这些是我的 Heroku 日志。

2013-10-15T19:51:45.703129+00:00 app[web.1]:  19: <% cities = SubjectGradeCity.includes(:city).collect(&:city).uniq %>
2013-10-15T19:51:45.703129+00:00 app[web.1]:  20: <% grades = SubjectGradeCity.includes(:grade).where(:city_id).collect {|s| {:name => s.grade.name, :id => s.grade.id}}.uniq %>
2013-10-15T19:51:45.703129+00:00 app[web.1]: ActionView::Template::Error (PG::Error: ERROR:  argument of WHERE must be type boolean, not type integer
2013-10-15T19:51:45.703129+00:00 app[web.1]: LINE 1: ...ade_cities".* FROM "subject_grade_cities"  WHERE ("subject_g...
2013-10-15T19:51:45.703129+00:00 app[web.1]:                                                              ^
2013-10-15T19:51:45.703129+00:00 app[web.1]: 17: <%= simple_form_for :assignments_filter , :html => {:id => "assignments_filter_form"}, :url => {:controller => "requests", :action => "assignments2"  } do |f| %>

这是我在视图中的代码


<% cities = SubjectGradeCity.includes(:city).collect(&:city).uniq %>
<% grades = SubjectGradeCity.includes(:grade).where(:city_id).collect {|s| {:name => s.grade.name, :id => s.grade.id}}.uniq %>
<% subjects = SubjectGradeCity.includes(:subject).where(:city_id).collect {|s| {:name => s.subject.name, :id => s.subject.id}}.uniq %>
<% grades.unshift({:name => "You have to select your city first", :id => ""}) if grades.empty? %>
<% subjects.unshift({:name => "You have to select your city first", :id => ""}) if subjects.empty? %>

请帮忙..

【问题讨论】:

    标签: ruby-on-rails heroku ruby-on-rails-3.2 heroku-postgres


    【解决方案1】:

    您的 where 子句没有调用任何可比较的内容,因此 PG 不知道要在结果中包含什么。 where 子句的计算结果必须为真/假。

    您可能正在寻找类似的东西:

    <% cities = SubjectGradeCity.includes(:city).collect(&:city).uniq %>
    <% grades = SubjectGradeCity.includes(:grade).where(:city_id => cities).collect {|s| {:name => s.grade.name, :id => s.grade.id}}.uniq %>
    <% subjects = SubjectGradeCity.includes(:subject).where(:city_id => cities).collect {|s| {:name => s.subject.name, :id => s.subject.id}}.uniq %>
    

    这样你就可以比较你的 where 子句,看看它是否包含在第一行的城市结果中......虽然我不确定这是否会起作用,因为你的第一行返回一组 SubjectGradeCity 对象而不是城市对象。但你也许可以从那里弄清楚?

    编辑:您还应该听取 NickM 的建议,将这些方法移出视图。他们绝对应该在模型层。

    【讨论】:

    • 哇,我知道了。如果我这样说呢? :city_id).collect {|s| {:name => s.grade.name, :id => s.grade.id}}.uniq %> :city_id).collect { |s| {:name => s.subject.name, :id => s.subject.id}}.uniq %>
    • 这行得通,但是你从哪里得到 :city_id 呢?另外,请参阅包含 NickM 答案的编辑,这绝对是最佳实践。
    • 是的,我同意。非常感谢,它奏效了。我正在使用表单提交给控制器,我不想使用模型。
    【解决方案2】:

    您永远不应该将数据库查询放在您的视图中......永远。我至少会将它们移至助手,但理想情况下它们应该在您的模型中定义。也就是说,Helios 的答案是好的,只要你把它放在你的模型中。

    【讨论】:

    • 绝对!大声喊叫。
    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多