使用 MongoDB 时对选项进行分组的最佳方式是什么?
当我尝试这种方法时,我正在使用 Mongoid:
<%= field.select :resource_id,
grouped_options_for_select(Resource.all.group_by{"resource_type_id"}.map {|k,m|
[m.first.title, m.first.id] }),:prompt => true %>
它给了我以下错误:
“5177e6a5359f105f89000001”的未定义方法“map”:Moped::BSON::ObjectId
当我在寻找:
<select>
<optgroup label="RT1"> <!-- RT1 is the name of resource type -->
<option value="5177e6a5359f105f89000001">Res1</option>
</optgroup>
</select>
此外,在控制台中的输出Resource.all.group_by{"resource_type_id"}
是
=> {"resource_type"=>[#<Resource _id: 5177e6a5359f105f89000001,
created_at: 2013-04-24 14:05:25 UTC, updated_at: 2013-04-24 14:54:14 UTC,
title: {"en"=>"Res1"}, slug: {"en"=>"res1"}, content:
{"en"=>"This is the content for First Resource."},
excerpt: {"en"=>"This is the content for First Resource."}, published: true,
resource_type_id: "5177e3ba359f10d345000004">]}
虽然预期的结果是
=> {"RT1"=>[#<Resource _id: 5177e6a5359f105f89000001,
created_at: 2013-04-24 14:05:25 UTC, updated_at: 2013-04-24 14:54:14 UTC,
title: {"en"=>"Res1"}, slug: {"en"=>"res1"}, content:
{"en"=>"This is the content for First Resource."},
excerpt: {"en"=>"This is the content for First Resource."}, published: true,
resource_type_id: "5177e3ba359f10d345000004">]}