ActiveRecord finder方法#where不过滤查询结果

时间:2017-11-12 19:34:19

标签: ruby-on-rails ruby-on-rails-4 scope class-method activerecord-relation

TL; DR 为什么我在使用课程方法调整活动和范围之间时会收到所有居民的列表?不活跃的居民?

我的应用程序使用'acts_as_tenant' gem。租户模型称为帐户,其中有许多管理居民的员工。

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

在我的常驻架构中有一个名为is_active的旗帜属性,其目的是区分活动和软删除的居民。您还可以看到添加的数据库约束。

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

我允许员工查看软删除的居民和提供有效/无效居民的标签式索引视图。

enter image description here

在我的Resident类中,我根据活动/非活动状态有以下类方法。

  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

我的问题是我在索引视图中收到活动和非活动标签中所有居民的列表。请注意,此视图页的两个图像中的姓氏相同。

enter image description here

我希望类方法分别只返回活跃或不活跃的居民。

我想知道为什么我没有根据我的查询类方法只接收活动或非活动的居民。

活动/非活动计数的类方法似乎按预期工作,这是一个峰值。

  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

到目前为止我的解读

包含的服务器日志以显示SQL

enter image description here

查看模板

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 %>

评论后更新

我的日志在下面,当我在浏览器中选择“非活动”选项卡时,日志中没有任何变化。我也没有在开发工具中看到js控制台的任何输出,除非我为详细输出设置了一个选项。我也将包括这个图像。 Logfile pt.1

Logfile pt.2

enter image description here

2 个答案:

答案 0 :(得分:1)

这对我有用

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

总结上面的链接:

  

如果您使用的是data-attribute方法,则必须将href属性设置为要打开的标签的ID。在JS版本中,您必须使用类似$('#myTab a[href="#profile"]').tab('show')的内容才能打开href值设置为#profile的选项卡

我的例子

在我添加的两个部分文件中

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

并且在包含部分内容的index.html.erb中,我将href值从#更改为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>

答案 1 :(得分:1)

这可能是UI / jQuery问题。查看是否在每个选项卡下呈现正确的数据。由于计数看起来是正确的。