3

在我的应用程序中,用户描述建筑物。用户应该能够使用分组选择指定建筑物所在的社区。模型看起来像:

class Building
  include Mongoid::Document
  belongs_to :neighborhood
end

class Neighborhood
  include Mongoid::Document
  field :name,         type: String, default: nil
  field :borough,      type: String, default: nil
  field :city,         type: String, default: nil
end

使用 simple_form,我试图生成一个分组选择,表示建筑物可能属于的社区列表。

= building_form.association :neighborhood, as: :grouped_select, collection: Neighborhood.where(city: city), group_method: :borough

理想情况下,它会创建如下内容:

Borough #1
  Downtown
  Uptown
Borough #2
  Suburbs
  ...

但是,我收到此错误:

undefined method `map' for "Borough #1":String

看起来它正在调用Neighborhood.borough.map,并且因为 String 没有map函数,所以它会出错。我该如何解决?

4

1 回答 1

7

我已经为此苦苦挣扎了一段时间,不幸的是,我希望从中获得的直观的“Rails”魔法association似乎并不存在。它使用的是底层 Rails grouped_collection_select,它似乎不能很好地处理对象/模型。

相反,它似乎可以更好地处理数组。根据此文档,集合输入应采用以下形式:

[
  ['group_name',
    [
      ['item-name','item-value'],
      ['item2-name','item2-value'],
      ...(more items)...
    ]
  ],
  ['group2_name',
    [
      ['item3-name','item3-value'],
      ['item4-name','item4-value'],
      ...(more items)...
    ]
  ],
  ...(more groups)...
]

MongoDB 模型自然不适合这种格式,所以我在我的Neighborhood类上编写了一个辅助方法:

def self.grouped_by_borough(city)
  groups = []
  Neighborhood.where(city: city).distinct(:borough).each_with_index do |borough, index|
    groups << [borough, Neighborhood.where(city: city, borough: borough)]
  end
  return groups
end

然后我的association样子:

= building_form.association :neighborhood, as: :grouped_select, collection: Neighborhood.grouped_by_borough(city), group_method: :last, option_key_method: :name, option_value_method: :id

这也会自动选择任何先前选择的邻域,便于“编辑”表格。

如果任何 Rails 表单/Mongoid 大师有更简洁的方式来处理这个问题,我很想听听。

于 2013-09-15T16:31:15.703 回答