[英]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? TL; DR : 当我使用类方法在活跃和不活跃居民之间进行分类时,为什么会收到所有居民的清单?
My application uses the 'acts_as_tenant' gem . 我的应用程序使用'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. 在我的居民模式中,有一个名为
is_active
的标志属性,目的是区分活跃居民和软删除居民。 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. 我允许员工查看软删除的居民并提供活动/不活动居民的选项卡式索引视图。
In my Resident class I have the following class methods to scope based on active/inactive state. 在我的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
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.
请注意,此视图页面的两个图像中的姓氏均相同。
I would prefer for the class method to only return active or inactive residents respectively. 我希望 class方法仅分别返回活动或不活动的居民。
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 包含服务器日志以显示SQL
View templates 查看模板
index.html.erb index.html.erb
<li class="active">
<a href="#" data-toggle="tab">
Active Residents
<span class="badge badge-purple">
<%= @active_resident_count %>
</span>
</a>
</li>
<li>
<a href="#" data-toggle="tab">
Inactive Residents
<span class="badge badge-dark">
<%= @inactive_resident_count %>
</span>
</a>
</li>
_active_residents.html.erb _active_residents.html.erb
<% if @active_residents.any? %>
<tbody>
<% @active_residents.reverse_each do |resident| %>
.
.
.
<% end %>
</tbody>
<% end %>
_inactive_residents.html.erb _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.
除非我为详细输出设置选项,否则在开发工具中我也看不到js控制台的任何输出。 I will include an image of this also.
我还将包括此图像。
This worked for me 这对我有用
Second comment in Yan Foto's answer to this SO post Yan Foto对此SO职位的回答中的第二条评论
To sum up the link above: 总结以上链接:
if you are using the
data-attribute
method you have to have thehref
attribute set to the ID of the tab you want to open.如果您使用的是
data-attribute
方法,则必须将href
属性设置为要打开的标签的ID。 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在JS版本中,您必须具有类似
$('#myTab a[href="#profile"]').tab('show')
,才能打开href值设置为#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
在存放部分内容的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
<span class="badge badge-purple">
<%= @active_resident_count %>
</span>
</a>
</li>
<li>
<a href="#my_tab2_id" data-toggle="tab">
Inactive Residents
<span class="badge badge-dark">
<%= @inactive_resident_count %>
</span>
</a>
</li>
</ul>
This could be a UI/jQuery issue. 这可能是UI / jQuery问题。 See if you are rendering the correct data under each tab.
查看是否在每个选项卡下呈现正确的数据。 Since the count looks correct.
由于计数看起来正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.