如何在ransack上使用fieldname设置默认条件? [英] Howto set a default condition with fieldname on ransack?

查看:42
本文介绍了如何在ransack上使用fieldname设置默认条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户和一个角色模型,两者都通过 habtm 关联,并且有一个与角色关联的论坛模型.在论坛的搜查搜索表单中,我想按具有特定角色的用户(按名称:主持人)进行过滤.

I have a user and a roles model, both are associated via habtm and there is a forum model associated to roles. In ransack search form for forums i want to filter by users who have a specific role (by name: moderator).

源看起来像这样:

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles, :join_table => :users_roles
  rolify

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles
  belongs_to :resource, :polymorphic => true

class Forum < ActiveRecord::Base
  has_many :roles, :as => :resource
  ...

<%= simple_form_for @forums, :html => { :class => 'form-horizontal' } do |f| %>
  ...
  <%= f.input :roles_users_id_in, 
              :collection => User.joins(:roles)
                                 .where(:roles => {:name => :moderator}) %>

是否可以将其组合到字段名称中,我是否需要使用自定义 preciated 设置某些内容,或者如何使用预设条件(role.name == :moderator)进行搜索?

Is it possible to combine this into the field name, do i need to setup something with custom preciated or how can i search with a preset condition (role.name == :moderator)?

更新

看来我必须使用自定义的掠夺者,我想出了以下内容:

It seesm like i have to use a custom ransacker and i came up with the following:

class Forum < ActiveRecord::Base
  ..
  ransacker :moderator_users, :formatter => proc { |v|
    Role.joins(:users).where(:roles => { :name => :moderator, 
                                         :resource_type => :forum}, 
                             :users => {:id => v}).map(&:resource_id)
  } do |parent|
    parent.table[:id]
  end

<%= search_form_for @q, :builder => SimpleForm::FormBuilder do |f| %>

    <%= f.input :moderator_users_in, 
                :collection => User.joins(:roles)
                                   .where(:roles => {:name => :moderator}),
    ...

通过上面的解决方法,有一个论坛 sql 查询和一个针对每个用户的查询.是否可以将其结合起来并将 sql 查询设置为这样的:

With the workaround above there is a forum sql query and a query for each user. Is it possible to combine this and set the sql query to something like this:

SELECT DISTINCT `forums`.* FROM `forums` 
  LEFT OUTER JOIN `roles` ON `roles`.`resource_id` = `forums`.`id` AND 
                  `roles`.`resource_type` = 'Forum'                  
  LEFT OUTER JOIN `users_roles` ON `users_roles`.`role_id` = `roles`.`id` 
  LEFT OUTER JOIN `users` ON `users`.`id` = `users_roles`.`user_id` 
WHERE `roles`.`name` = 'responsible' AND `users`.`id` IN (1,3,6)

另见:https://github.com/ernie/ransack/issues/103

推荐答案

我不是 100% 确定我收到了这个问题,而且它有点老了,但我还是会尝试一下:

I'm not 100% sure that I get this question and it's a little old, but I will try it anyway:

因此,您有想要搜索的模型,并且您有一直想要应用的条件.现在,我想到的第一件事就是控制器中结果的 where 条件,如下所示:

So, you have model that you want to search and you have condition that you always want to apply. Now the first thing that comes into my mind is just your where condition on the result in your controller, like this:

@q = Forum(params[:q])
@forums = @q.result.where("role_id = ?", 1) # the number of course should be the id of the moderator object

或者您可以尝试在搜索表单中设置隐藏字段:

Or you could try a hidden field in your search form:

<%= f.select(:role_name_matches, Role.pluck(:name), :as => :hidden, :input_html => { :value => "moderator" } ) %>

如果你的意思是别的,但仍然需要回答这个问题,请向我解释你的意思.我在洗劫方面有一些经验,希望能提供帮助.

If you mean something else and still need this answered please explain to me what you mean. I have quite some experience with ransack and would like to help.

这篇关于如何在ransack上使用fieldname设置默认条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆