Rails 汗! 這個該死的 Active Record Custom Query 到底要怎麼搞掂它 (我只是想做多個下拉選單的查詢表單)

idarfan · 2012年04月24日 · 最后由 idarfan 回复于 2012年04月24日 · 2645 次阅读

我也試著去把 msyql 的解法給做出來。底下是我寫出來的 mysql ,但這對問題好像沒有幫助因為我也不知道如何將它塞進去 rails 當一個方法用

SELECT  SI.incomelevel_id, SG.graduated_id,  SH.howuknowu_id, SM.mostimportchoice_id, ST.student_id, ST.cname, ST.email
FROM students AS ST, student_incomelevelships AS SI, student_graduatedships AS SG, student_howuknowusships AS SH, student_mostimportchoiceships AS SM
WHERE ST.student_id = SI.student_id  AND  ST.student_id =SG.student_id  AND  ST.student_id = SH.student_id AND ST.student_id= SM.student_id
AND  SI.incomelevel_id = 1
AND  SG.graduated_id =  1
AND  SH.howuknowu_id = 1
AND  SM.mostimportchoice_id = 2
GROUP BY ST.student_id



#加入學歷的選項
def money
    @level = Incomelevel.all.map{|im|[im.reason_desc , im.id]}
    @grat = Graduated.all.map{|gr|[gr.reason_desc , gr.id]}
    render :layout =>"test_layout"
end



def show_money
    start_at = Time.parse(params[:start_at])
    end_at = Time.parse(params[:end_at])   
    level = params[:level].to_i
    grat = params[:grat].to_i   
    @students = Student.all(:from => "student_incomelevelships AS si AND student_graduatedships AS sg" ,
      :conditions => ["si.incomelevel_id = ? AND sg.graduated_id = ?", level , grat] ,    
      :joins => "INNER JOIN
        students ON students.id = si.student_id AND students ON students.id = sg.student_id AND
        students.created_at BETWEEN DATE('#{start_at.strftime("%Y/%m/%d")}')
        AND DATE('#{end_at.strftime("%Y/%m/%d")}')")   
  end



viewer 的部份是這樣寫的:

<% content_for :header do %>
  <script>
    jQuery(function($){
      //alert("ewe");
      $(".date_pick").datepicker();
    })
  </script>
<% end %>
<form action="show_money" methods="get">
  <input type="text" name="start_at" value="<%= (Time.now - 1.year).strftime("%Y/%m/%d") %>" class="date_pick"/>
  <input type="text" name="end_at" value="<%= Time.now.strftime("%Y/%m/%d") %>" class="date_pick"/>
  <select name="level">
    <% @level.each do |level| %>
      <option value="<%= level[1] %>"><%= level[0] %></option>
    <% end %>
  </select>
  <select name="grat">
    <% @grat.each do |grat| %>
      <option value="<%= grat[1] %>"><%= grat[0] %></option>
    <% end %>
  </select>
  <input type="submit" value="send"/>
</form>



joins 的用法部份,我手邊有的資源也相當有限。我比對我 mysql 的語法大致上認為應該沒有錯,只是 AND 與 OR 我目前對其作用不甚明白 但這樣我在做查詢時。屢屢出現底下的錯誤訊息 (看起來我的 joins 並沒有用對,能否請您指點我 感恩)

ActiveRecord::StatementInvalid in StudentsController#show_money

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND student_graduatedships AS sg INNER JOIN
        students ON students.id = si' at line 1: SELECT `students`.* FROM student_incomelevelships AS si AND student_graduatedships AS sg INNER JOIN
        students ON students.id = si.student_id AND students ON students.id = sg.student_id AND
        students.created_at BETWEEN DATE('2011/04/24')
        AND DATE('2012/04/24') WHERE (si.incomelevel_id = 1 AND sg.graduated_id = 6)

Rails.root: /home/kojendirect/projects/employee
Application Trace | Framework Trace | Full Trace

app/controllers/students_controller.rb:157:in `show_money'

Request

Parameters:

{"start_at"=>"2011/04/24",
 "end_at"=>"2012/04/24",
 "level"=>"1",
 "grat"=>"6"}



後來我查到一些錯誤,重新編修了 show_money 方法 詳見附圖 http://i1210.photobucket.com/albums/cc408/idarfan/Ruby%20on%20Rails/2012-04-24170724.png

#加入學歷的選項
  def money
    @level = Incomelevel.all.map{|im|[im.reason_desc , im.id]}
    @grat = Graduated.all.map{|gr|[gr.reason_desc , gr.id]}
    render :layout =>"test_layout"
  end

  def show_money
    @graduateds = Graduated.all
    start_at = Time.parse(params[:start_at])
    end_at = Time.parse(params[:end_at])
    level = params[:level].to_i
    grat = params[:grat].to_i
    @students = Student.all(:from => "student_incomelevelships AS si, AND student_graduatedships AS sg" ,
      :conditions => ["si.incomelevel_id = ? AND sg.graduated_id = ?", level , grat] ,
      :joins => "INNER JOIN
        students ON (students.id = si.student_id AND students.id = sg.student_id AND
        students.created_at BETWEEN 
        DATE('#{start_at.strftime("%Y/%m/%d")}') AND
        DATE('#{end_at.strftime("%Y/%m/%d")}'))")
  end

怪怪我 rails c 裏查,我這個是可以用的啊 StudentGraduatedship.find(1) => #

需要 登录 后方可回复, 如果你还没有账号请 注册新账号