2016-11-15 31 views
1

我期待從陣列得到一個ID,當得到它顯示了這個行動上添加Order.newRails的預期,得到了陣列

顏色(#70131258622840)預期,得到了陣列(#70131401174240 )

有人有什麼想法爲什麼?

產品型號

has_many :colorships 
has_many :colors, through: :colorships 

顏色模型

has_many :colorships 
has_many :products, :through => :colorships 

產物控制器

def new 
    Product.New 
    @dropdown = @product.colors.collect { |co| [co.name, co.id] } 
end 

def show 
    Product.find(params[:id]) 
    color = product.colors.select { |i| [i.id] } 
end 

def add 
    product = Product.find(params[:id]) 
    if product 
    color = product.colors.select { |i| [i.id] } 
    if order.nil? # create new order 
     order = Order.new 
     order.product = product 
     order.color = color   
    end 
    end 
end 
+0

顯示整個回溯有助於他人查看問題的存在位置,因此我鼓勵您顯示確切的錯誤。 – uday

回答

3
color = product.colors.select { |i| [i.id] } 

這條線給你一組顏色,而不是顏色。這將是更自然的

color = product.colors.select { |i| i.id } 

select給你一個數組爲好,即使在這種情況下,一個元素。 find給你你想要的元素或nil代替

color = product.colors.find { |i| i.id } 
+0

就是這樣。非常感謝你@ursus – jjplack

0

就像你說的,你需要的ID陣列。你也可以通過product.colors.ids得到。

這將返回顏色的ID數組。

相關問題