2016-12-05 49 views
0

我想通過屬性設置將它們分配給我的產品屬性,即我將Size設置爲屬性集並希望在其下分組Small,Medium,Large等。如何按列分組收藏

這是我目前擁有的代碼,但我得到了一些錯誤

- Property.all.group_by(&:property_set_id).each do |property_set, properties| 
    h3= property_set.name 
- properties.each do |property| 
    = property.property_set.name 
    .property_form.left.span-9.last 
    - checked = property_set.name && property_set.properties.include?(property) 
    label.mdl-switch.mdl-js-switch.mdl-js-ripple-effect for=property.name 
     = check_box_tag "prototype[property_ids][]", property.id, checked, :class => 'mdl-switch__input', :id => property.name 
     span.mdl-switch__label= property.name 


    def edit 
    @properties = Property.all 
    @property_set = PropertySet.includes(:properties).find(params[:id]) 
    end 

回答

1

在這一行分組property_set後,將成爲id

- Property.all.group_by(&:property_set_id).each do |property_set, properties| 

一些例如:

{ 1 => [<#Property>], 2 => [<#Property>] } 

所以你不能在整數值上調用.name。你需要從db找到它

h3= PropertySet.find(property_set).try(:name) 
# or 
h3= properties.find{ |p| p.property_set_id == property_set}.property_set.try(:name) # to avoid query