1

Given these associations...

class User < ActiveRecord::Base
  has_many :orders

  has_many :followers, through: :follower_follows, source: :follower
  # follower_follows "names" the Follow join table for acessing through the follower association
  has_many :follower_follows, foreign_key: :followee_id, class_name: "Follow"

  has_many :followees, through: :followee_follows, source: :followee
  # followee_follows "names" the Follow join table for accessiing through the followee association
  has_many :followee_follows, foreign_key: :follower_id, class_name: "Follow"  
end

class Order < ActiveRecord::Base
  belongs_to :user
end

... and a controller...

class OrdersController < ApplicationController
  def index
    @q = Order.ransack(params[:q])
    @orders = @q.result.includes(:user, :followers)
    # I'am not sure if the includes method is corrects
  end
end

... in my views ...

<%= search_form_for @q do |f| %>
  <%= f.label :user_id_eq %>
  <%= f.search_field :user_id_eq %>

  <%= f.submit "search" %>
<% end %>

I've this scenario:

User with ID = 1 => manager role

User with ID = 2 => follower of user_id=1

User with ID = 3 => follower of user_id=1

# select * from follows;
 id | follower_id | followee_id |         created_at         |         updated_at         
----+-------------+-------------+----------------------------+----------------------------
  1 |           2 |           1 | 2017-12-09 22:30:03.299006 | 2017-12-09 22:30:03.299006
  2 |           3 |           1 | 2017-12-09 22:30:03.304564 | 2017-12-09 22:30:03.304564

The question is:

howto setup ransack to find by f.search_field: user_id_eq (or whatever) to get all order items owned by user_id = 1 plus all orders items owned by of user_id = 2 and plus user_id = 3 ?

Many thanks for your support.

2 Answers2

1

After your answer, I've try with:

Order.ransack(user_followees_id_eq: 17).result

It seems like this works as I wanted.

0
Order.ransack(user_id_eq_any: [1,2,3]).result

f.search_field :user_id_eq_any

Rails + Ransack - Drop Down List Collection?

Refer above answer if you want it make input as select, add 'multiple' attribute for allowing multiple selection