Howto set a default condition with fieldname on ransack?

送分小仙女□ 提交于 2019-12-23 12:18:50

问题


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).

Source looks like this:

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}) %>

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)?

Update

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}),
    ...

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)

See also: https://github.com/ernie/ransack/issues/103


回答1:


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

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.



来源:https://stackoverflow.com/questions/10969678/howto-set-a-default-condition-with-fieldname-on-ransack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!