2016-07-23 49 views
0

我試圖從tints實體獲取front, rear, side屬性的值。無法訪問表屬性的值

廠家{id, name}
模式{id, manufacturer_id, name}
色調{id, manufacturer_id, model_id, front, side, rear}

這是我tints_controller.rb

def generate 
    @manufacturers = Manufacturer.find(params[:tint][:manufacturer_id]) 
    @models = Model.find(params[:tint][:model_id]) 
    @price_front = Price.find(params[:price][:price_front]) 
    @price_rear = Price.find(params[:price][:price_rear]) 
    @measurements = Tint.where('manufacturer_id' => @manufacturers).where('model_id' => @models) 
    end 

我收到的時候我試圖做<%= @measurements.rear %>generate.html.erb

undefined method `rear' for #<Tint::ActiveRecord_Relation:0x0000000c628610> 
錯誤

下面是調試屏幕

Request 

Parameters: 

    {"utf8"=>"✓", 
    "authenticity_token"=>"jfV6SUkNdKWO1f1fyzqS4eLbvszjDPNm2E81M90lnvPArYScSjcjQlxsJdcuqQMJ+mSecCsyVQlhz2cpAH1DUw==", 
    "tint"=>{"manufacturer_id"=>"6", "model_id"=>"3"}, 
    "price"=>{"price_front"=>"1", "price_rear"=>"2"}, 
    "commit"=>"Submit"} 

然而,當我做<%= @measurements.inspect %>,它顯示出低於行,指示在tints_controller@measurements正確定義。它顯示它可以通過搜索正確的model_idmanufacturer_id從表中獲得整行。不過,我不能直接做<%= @measurements.front =>

#<ActiveRecord::Relation [#<Tint id: 1, manufacturer_id: 6, model_id: 3, front: 40, side: 50, rear: 60, created_at: "2016-07-22 04:14:49", updated_at: "2016-07-22 04:14:49">]> 

我做了什麼,以獲得front, side, rear值作爲int: 我試圖做<%= @measurements[:front] =><%= @measurements[:id][:front] =>,我仍然得到錯誤。

回答

0

當使用where自ActiveRecord選擇,則返回ActiveRecord_Relation對象,該對象是總是相關對象的數組,因爲多個對象可以從where查詢返回。

您需要可以指定Tint在視圖中選擇:

@measurements.first.rear 

,或者您需要修改控制器只能選擇被返回的第一個對象:

@measurements = Tint.where('manufacturer_id' => @manufacturers).where('model_id' => @models).first