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

1 answer

  • answered 2017-11-13 14:36 sirramongabriel

    This worked for me

    Second comment in Yan Foto's answer to this SO post

    To sum up the link above:

    if you are using the data-attribute method you have to have the href attribute set to the ID of the tab you want to open. In JS version you have to have something like $('#myTab a[href="#profile"]').tab('show') in order to open a tab with href value set to #profile

    My example

    in both partial files I added

    <div class="tab-pane fade in active" id="my_tab1_id">

    and in index.html.erb which houses the partials I changed the href value from # to my_tab_id

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