2013-10-15 45 views
0

我得到這個heroku錯誤,PG ::錯誤:錯誤:WHERE的參數必須是類型布爾值,而不是整數

這些是我的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? %> 

幫助,請..

回答

4

你的where子句中並沒有叫任何與比較,所以PG不知道在結果中包含什麼。 A 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的建議,將這些方法移出視野。他們絕對應該在模型層面。

+1

哇,我知道有一個想法。如果我這樣說? <%grades = SubjectGradeCity.includes(:grade).where(:city_id =>:city_id).collect {| s | {:name => s.grade.name,:id => s.grade.id}}。uniq%> <%subject = SubjectGradeCity.includes(:subject).where(:city_id =>:city_id).collect {| S | {:name => s.subject.name,:id => s.subject.id}}。uniq%> – ben

+1

這會工作,但你從哪裏得到:city_id?另外,請參閱包含NickM答案的編輯,這絕對是最佳做法。 –

+0

是的,我同意。非常感謝,它工作。我正在使用表單向控制器提交,我不想使用模型。 – ben

2

你永遠不應該把數據庫查詢放在你的視圖中......永遠。我至少會把他們轉移到幫手上,但他們應該在你的模型中進行理想的定義。說,只要你把它放在你的模型中,Helios的答案就是好的。

+0

絕對!好喊。 –

相關問題