Postgresql 使值在“其他”之后的下拉列表中首先显示

Postgresql 使值在“其他”之后的下拉列表中首先显示,postgresql,ruby-on-rails-4,Postgresql,Ruby On Rails 4,我有一个行业集合,我使用以下代码在下拉列表中显示: <%= ff.select :area_of_business_id, Industry.all.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %> 而且,我希望先出现娱乐节目,然后我希望将顺序更改为: Entertainment Airlines Br

我有一个行业集合,我使用以下代码在下拉列表中显示:

<%= ff.select :area_of_business_id, Industry.all.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>
而且,我希望先出现娱乐节目,然后我希望将顺序更改为:

Entertainment

Airlines

Broadcasting

Chemicals

Insurance

最干净的方法是什么?

是,如下所示。假设控制器中有一个实例变量:

@option_values = Industry.order("name <> 'Entertainment', name asc")
                         .pluck(:name, :id)
@option\u values=Industry.order(“名称‘娱乐’,名称asc”)
.pluck(:name,:id)
现在做

<%= ff.select :area_of_business_id, @option_values, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>


该部分
顺序(“name'Entertainment',name asc”)
将以
name='Entertainment'
的顺序将记录放在第一位,然后以降序将其余部分放在第二位。

可能是,如下所示。假设控制器中有一个实例变量:

@option_values = Industry.order("name <> 'Entertainment', name asc")
                         .pluck(:name, :id)
@option\u values=Industry.order(“名称‘娱乐’,名称asc”)
.pluck(:name,:id)
现在做

<%= ff.select :area_of_business_id, @option_values, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>


该部分
顺序(“name'Entertainment',name asc”)
将把记录与
name='Entertainment'
放在一起,然后将其余部分按降序排列。

Rails只支持开箱即用选项的优先级。在所有其他情况下,您应该手动执行此操作,并使用类似以下内容:

<% industries = [Industry.find_by(name: 'Entertainment')] + [Industry.where.not(name: 'Entertainment')] %> 
<%= ff.select :area_of_business_id, industries.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>


但我建议在
行业
模型中添加额外的字段,以便在列表中指定位置并按此字段排序。

Rails仅支持开箱即用选项的优先级。在所有其他情况下,您应该手动执行此操作,并使用类似以下内容:

<% industries = [Industry.find_by(name: 'Entertainment')] + [Industry.where.not(name: 'Entertainment')] %> 
<%= ff.select :area_of_business_id, industries.map { |x| [x.name, x.id] }, {include_blank: "Select an industry"}, class: 'droplist default-droplist required' %>

但我建议在
行业
模型中添加额外的字段,以指定列表中的位置并按此字段排序