Search code examples
ruby-on-railsruby-on-rails-4scopeclass-methodactiverecord-relation

ActiveRecord finder method #where not filtering query results


TL;DR: Why am I receiving a list of all resident's when I'm using class methods to scope between active & inactive residents?

My application uses the 'acts_as_tenant' gem. The tenant model is called Account, which has many employees who manage residents.

class Account < ActiveRecord::Base
  before_create :set_subdomain

  has_one  :admin, class_name: 'Employee', foreign_key: :account_id, 
    dependent: :destroy, inverse_of: :account

  has_many :employees, class_name: 'Employee', foreign_key: :account_id, 
    dependent: :destroy, inverse_of: :account

  has_many :residents
  has_many :invites
  has_many :daily_summaries
  has_one  :subscription
  .
  .
  .
end

In my resident's schema there is a flag attribute named is_active, it's purpose being to differentiate between active and soft-deleted residents. You can also see the db constraints added.

create_table :residents do |t|
  t.integer    :account_id, null: false
  t.integer    :employee_id, null: false
  t.boolean    :is_active, default: true, null: false
  .
  .
  .
end

I allow employees to view soft-deleted residents & offer a tabbed index-view of both active/inactive residents.

enter image description here

In my Resident class I have the following class methods to scope based on active/inactive state.

  class Resident < ActiveRecord::Base
    acts_as_tenant(:account)
    .
    .
    .
    def self.inactive_count(current_account)
      where({ account_id: current_account.id, is_active: false }).count
    end

    def self.default(current_account)
      where({ account_id: current_account.id, is_active: true })
    end

    def self.inactive(current_account)
      # where({ account_id: current_account.id, is_active: false })
      where("account_id = ? AND is_active = ?", current_account.id, false).first 
    end

    def archive
      update_attribute(:is_active, false)
    end
    .
    .
    .
  end

My problem is that I am receiving a list of all residents in both the active and inactive tabs in my index-view. Note the last name is the same in both images of this view page.

enter image description here

I would prefer for the class method to only return active or inactive residents respectively.

I'm wanting to find out why I'm not receiving only active or only inactive residents based on my query class methods.

The class methods for active/inactive count appear to be working as expected, here is a peak.

  def index
   @inactive_residents = Resident.inactive(current_account)
   @inactive_resident_count = Resident.inactive_count(current_account)
   @active_residents = Resident.default(current_account)
   @active_resident_count = Resident.active_count(current_account)
  end

My reading so far to solve:

Server logs included to show SQL

enter image description here

View templates

index.html.erb

  <li class="active">
    <a href="#" data-toggle="tab">
      Active Residents&nbsp;&nbsp;
      <span class="badge badge-purple">
        <%= @active_resident_count %>
      </span>
    </a>
   </li>
   <li>
     <a href="#" data-toggle="tab">
       Inactive Residents&nbsp;&nbsp;
       <span class="badge badge-dark">
         <%= @inactive_resident_count %>
       </span>
     </a>
   </li>

_active_residents.html.erb

<% if @active_residents.any? %>
  <tbody>
    <% @active_residents.reverse_each do |resident| %>
      .
      .
      .
    <% end %>
  </tbody>
<% end %>

_inactive_residents.html.erb

<% unless @inactive_residents.nil? %> 
  <% @inactive_residents.reverse_each do |resident| %>
    .
    .
    .
  <% end %>
<% end %>

Update after comment

My logs are below, nothing changes in the log when I select the 'inactive' tab in the browser. I also don't see any output from js console in dev tools unless I set an option for verbose output. I will include an image of this also. Logfile pt.1

Logfile pt.2

enter image description here


Solution

  • This could be a UI/jQuery issue. See if you are rendering the correct data under each tab. Since the count looks correct.