2016-11-15 56 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数组。

相关问题