如何按列对集合进行分组

时间:2016-12-05 11:14:26

标签: ruby-on-rails collections grouping slim-lang

我正在尝试按照下面的属性集对我的产品属性进行分组,即我将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 个答案:

答案 0 :(得分: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