鉴于这些联想......
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
......还有一个控制器......
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
......在我看来......
<%= search_form_for @q do |f| %>
<%= f.label :user_id_eq %>
<%= f.search_field :user_id_eq %>
<%= f.submit "search" %>
<% end %>
我有这个场景:
ID = 1 =>经理角色的用户
ID = 2的用户=> user_id = 1的关注者
ID = 3的用户=> 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
问题是:
如何设置ransack以通过f.search_field:user_id_eq(或其他)查找以获取user_id = 1所拥有的所有订单商品以及user_id = 2和plus user_id = 3所拥有的所有订单商品?
非常感谢您的支持。
Order.ransack(user_id_eq_any: [1,2,3]).result
f.search_field :user_id_eq_any
Rails + Ransack - Drop Down List Collection?
如果您想将输入选为select,请参考上面的答案,添加'multiple'属性以允许多个选择
在你回答之后,我试着:
Order.ransack(user_followees_id_eq: 17).result
看起来这像我想要的那样有效。