2012-03-14 58 views
0

我已經三個表「硬件」,「倉庫」和「品牌」和表refernced在一起,以這種方式:硬件< - >倉庫硬件< - >品牌轉換複雜的MySQL選擇到Rails 3.1

內倉庫視圖,我有一個搜索下拉菜單,以這種方式工作:您從菜單中選擇一個「品牌」,當您單擊提交時,應用程序將返回與您選擇的關聯的所有倉庫對象。 問題在於倉庫表與我之前展示的品牌無關,因此它沒有加入倉庫和品牌的任何外鍵。 所以我必須想盡了辦法來這個MySQL查詢轉換中Rails應用程序:

SELECT W1 * FROM倉庫W1,硬件H1其中w1.hardware_id = h1.id和h1.brand_id = 3

這是我的代碼:

class WarehousesController < ApplicationController 
def search 
     generate_selects true 
     respond_to do |format| 
      format.html # search.html.erb 
      format.js {render :partial => 'get_models'} 
      format.xml { render :xml => @warehouse } 
     end 
    end 

    def search_result 
     if request.post? && params[:from_form] 
      session[:sa_query_flatten] = sa_query.flatten #Memorizzo sulla session per riutilizzarla in "aggregate_by_position" e will_paginate per page > 1 
     end 
     conditions = session[:sa_query_flatten] 
     logger.debug "QUERY: #{conditions} SESSION:#{session[:sa_query_flatten].inspect}" 
     @products = Warehouse.find(:all,:conditions => conditions) 
     @search_count = @products.count 
     @products = @products.paginate(:page => params[:page], :per_page => 100) 
     @search_start = ((params[:page].to_i - 1)* 100) + 1 
     @search_end = (params[:page].to_i * 100) - 1 
     if @search_start < 0 
     @search_start = @search_start + 100 
     end 
     if @search_end < 0 
     @search_end = @search_end + 100 
     end 
     if @search_end > @search_count 
     @search_end = @search_count 
     end 
     respond_to do |format| 
      format.html 
      format.js 
     end 
    end 

def generate_selects(search) 
     if !search 
      @hardwares ||= Hardware.where({:brand_id => @warehouse.hardware.brand_id}).map{|hw| [hw.model,hw.id]}.sort 
      if session[:user_role] != "administrator" 
       @ingress_transport_documents = IngressTransportDocument.filtered_by_registry(session[:registry_id]).map{|itd| [itd.number,itd.id]}.sort 
      else 
       @ingress_transport_documents = IngressTransportDocument.all.map{|itd| [itd.number,itd.id]}.sort 
      end 
     end 
      @brands = Brand.all.map{|br| [br.name,br.id]}.sort 
      @positions = Position.all.map{|pos| [pos.name,pos.id]}.sort 
      @states = State.all.map{|st| [st.name,st.id]}.sort 
      @logicalWH = Logicalwarehouse.all.map{|lw| [lw.name,lw.id]}.sort 
      @extra = Extra.all.map{|ex| [ex.name,ex.id]}.sort 
      @registries = Registry.all.map{|cs| [cs.name,cs.id]}.sort 

    end 


    def sa_query 
     if params[:warehouse] 
      session[:params_warehouse] = params[:warehouse] 
     else 
      params[:warehouse] = session[:params_warehouse] 
     end 
     query = String.new 
     query_array = Array.new 
     if params[:enable_asset] == "1" 
      query << "asset LIKE ? AND " 
      query_array.push(params[:asset] + "%") 
     end 
     if params[:enable_serial] == "1" 
      query << "serial LIKE ? AND " 
      query_array.push params[:serial] + "%" 
     end 
     if params[:enable_brand] == "1" 
      query << "**brand** => = ? AND " 
      query_array.push Brand.find(params[:brand]).name # Qui va messo brand, non brand_value 
     end 
     if params[:enable_model] == "1" 
      query << "hardware_id.model = ? AND " 
      query_array.push params[:model] 
     end 
     if params[:enable_position] == "1" 
      query << "position_id = ? AND " 
      query_array.push params[:position] 

     end 
     if params[:enable_logicalwarehouse] == "1" 
      query << "logicalwarehouse_id = ? AND " 
      query_array.push params[:logicalwarehouse] 
     end 
     if query.index("AND ",-4) != nil 
     query = query[0,query.length-5] 
     end 
     [query,query_array] 
    end 

Mysql的等待品牌作爲查詢的輸入參數,有什麼建議?

回答

0

如果您使用rails,我的建議是退一步,嘗試在rails本身中執行更多查詢,並使用盡可能最佳的模型設置來完成此操作。

如下我會組織你的3種型號:那麼

class Warehouse < ActiveRecord::Base 
    has_many : brands 
    has_many : hardwares, :through => brands 
end 


class Brand < ActiveRecord::Base 
    belongs_to :hardware 
    belongs_to :warehouse 
end 

class Hardware < ActiveRecord::Base 
    has_many :brands 
    has_many :warehouses, :through => brands 
end 

這種結構將讓你做你想要的Brand.hardwaresBrand.warehouses查詢。只需在find中添加您想要的品牌即可。

+0

好吧,我應該在倉庫中添加一個外鍵以將倉庫表與品牌鏈接起來嗎? – Marco 2012-03-15 07:05:12

+0

是的,brand_id在倉庫表中。 – 2012-05-01 02:56:30