简体   繁体   中英

Rails: How do I exclude records with entries in a join table?

I have a rails 4.1 app with a work_tasks table and a work_task_statuses table.

They are joined together on work_task_statuses.id = work_tasks.id.

In my controller, I want to pull up only the work_tasks that do not have any records in the work_task_statuses table.

Here is models/work_tasks.rb:

require 'rails'
require 'filterrific'



class WorkTask < ActiveRecord::Base

  has_many :work_task_statuses

  scope :without_work_task_statuses, includes(:work_task_statuses).where(:work_task_statuses => { :id => nil })    

end

Here is work_tasks_controller.rb:

require 'builder'
require 'will_paginate'
include ActionView::Helpers::NumberHelper

class WorkTasksController < ApplicationController
  before_action :authenticate_user!

  def list
  end

  def index
    @filterrific = Filterrific.new(WorkTask, params[:filterrific])

    @filterrific.select_options = {
      sorted_by: WorkTask.options_for_sorted_by,
      with_work: WorkTask.options_for_select
    }

    @work_tasks WorkTask.filterrific_find(@filterrific).page(params[:page]).without_work_task_statuses(@work_tasks)

    respond_to do |format|
      format.html
      format.js
    end
  end

  def reset_filterrific
    # Clear session persistence
    session[:filterrific_work_tasks] = nil
    # Redirect back to the index action for default filter settings.
    redirect_to action: :index
  end
end

I am getting this error:

undefined method `call' for #<WorkTask::ActiveRecord_Relation:0x007fa0eeab4870>

How do I make the controller exclude work tasks that have records in work_task_statuses?

With the help of @jkeuhlen (who helped me to realize that the scope syntax had changed in rails 4) and via some further research I solved my problem.

For the benefit of future searchers, here is the final working code:

Model:

scope :with_work_task_statuses, -> {where.not(:id => WorkTaskStatus.select(:work_task_id).uniq)
  }

Controller:

@work_tasks = WorkTask.filterrific_find(@filterrific).page(params[:page]).with_work_task_statuses

Here is a blog post that explains it in more detail: http://www.lauradhamilton.com/how-to-filter-by-multiple-options-filterrific-rails

This question is related to this other SO question .

Using the information on the highest voted answer from that post and applying it to your question:

WorkTask.includes(:work_task_statuses).where( :work_task_statuses => { :work_task_id => nil } )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM