如何按列对集合进行分组
How to group collection by columns
我正在尝试按下面的 属性 集对我的产品属性进行分组,即我将尺寸设置为 属性 集,并希望将其分组为小、中、大等..
这是我目前拥有的代码,但我遇到了一些错误
- 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
分组后的这一行 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
我正在尝试按下面的 属性 集对我的产品属性进行分组,即我将尺寸设置为 属性 集,并希望将其分组为小、中、大等..
这是我目前拥有的代码,但我遇到了一些错误
- 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
分组后的这一行 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