2015-10-06 38 views
0

我有一個下拉列表,我嘗試用ActiveRecord的結果填充。我想我不明白如何訪問我檢索的值,因爲下面的代碼填充我的下拉列表中的值,如使用ActiveRecord中的值填充下拉列表

#<Product:0x007f1f488565b0>這顯然不是我想要的。

<%= f.select :accessory, options_for_select(Product.select(:item_number, :id).where(:accessory=> 't') {|c| [ c.item_number, c.id ] }), {include_blank: true}, { :class => 'form-control'} %> 

如何使用項目編號和ID填充下拉列表?這個查詢中沒有涉及任何關係。我只想要一個附件標有「t」的產品清單。

回答

0

你是如此接近:-)

爲options_for_select參數應與對象的枚舉,其響應的對象:第一和:最後

你傳入

Product.select(:item_number, :id).where(:accessory=> 't') 

作爲參數。 但我的猜測是,你想傳遞

Product.select(:item_number, :id).where(:accessory=> 't') 
    .map{|c| [ c.item_number, c.id ] }) 

你根本都忘了把「地圖」。

Ruby允許你傳遞任何方法的塊,這就是爲什麼你沒有得到任何警告或錯誤,但塊將被忽略。

+0

謝謝馬克! 'Product.select(:item_number,:id).where(:accessory =>'t')。map {| c | [c.item_number,c.id]}'工作。 – mack