2017-12-19 8 views
1

diese Verbände gegeben ...Rails plündern und Selbst Join Verbände Abfrage

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 

... und einen 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 meine Ansichten ...

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

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

ich habe dieses Szenario:

Benutzer mit ID = 1 => Manager-Rolle

Benutzer mit ID = 2 => Anhänger user_id = 1

Benutzer mit ID = 3 => Anhänger 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 

Die Frage ist:

Howto setup ransack, um nach f.search_field zu suchen: user_id_eq (oder was auch immer), um alle Auftragspositionen zu erhalten, die zu user_id = 1 gehören, sowie alle Auftragspositionen, die von user_id = 2 und plus user_id = 3 gehören?

Vielen Dank für Ihre Unterstützung.

Antwort

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

f.search_field :user_id_eq_any 

Rails + Ransack - Drop Down List Collection?

oben Antwort finden, wenn Sie es als Eingang wählen möchten, fügen Sie 'multiple' Attribut für Mehrfachauswahl ermöglicht

0

Nach Ihrer Antwort, habe ich versuche, mit:

Order.ransack(user_followees_id_eq: 17).result 

Es scheint, als ob dies funktioniert, wie ich wollte.