【发布时间】:2015-07-07 06:35:40
【问题描述】:
我有具有role(字符串)属性的用户模型。我想按此角色属性过滤我的用户。
我使用filterific gem 进行过滤。
这是我的index 操作:
def index
@filterrific = initialize_filterrific(
User,
params[:filterrific],
select_options: {
with_role: User.options_for_select
},
) or return
@users = @filterrific.find.page(params[:page])
end
options_for_select 方法定义在哪里:
# user.rb
def self.options_for_select
order('LOWER(role)').map { |e| [e.role, e.id] }
end
在我看来:
<%= f.select(
:with_role,
@filterrific.select_options[:with_role],
{ include_blank: '- Any -' }
) %>
我的 user.rb 中有一个范围:
scope :with_role, lambda { |roles|
where(role: [*roles])
}
我只有五个角色,但是在我的选择中每个角色出现了很多次,所以我不知道如何在options_for_select 方法中返回唯一角色。
其次,options_for_select 在每个选项标签中返回用户 ID 作为值,但我希望它返回角色本身。
【问题讨论】: