やりたいこと
中間テーブルを含めた検索をかけたいです。
なんとか実装までは漕ぎ着けたんですが、他に良い方法(または間違っているなど)など、ご指摘いただると幸いです。
試していること
ruby
1def index 2# events_controller.rb 3 scope = Event.includes(:users, :creator, :updater) 4 scope = scope.select { |e| (e.publish? or e.joins?(current_user)) } 5 scope = Event.revive_active_record(scope) 6 7 # ここから検索条件 8 @q = scope.ransack(params[:q]) 9 ・ 10 ・ 11 ・ 12end
ruby
1# event.rb 2has_many :attendee_events, dependent: :destroy 3has_many :users, through: :attendee_events 4 5def joins?(user) 6 users.exists?(user.id) 7end 8 9def self.revive_active_record(arr) 10 arr.first.class.where(id: arr.map(&:id)) 11end
回答1件
あなたの回答
tips
プレビュー