[英]Complex postgres query in rails
Currently having a query for filter with parameters premium_amount_lower_range, premium_amount_upper_range, application_date_lower_range, application_date_upper_range, insurance_type_id
当前正在查询具有参数premium_amount_lower_range, premium_amount_upper_range, application_date_lower_range, application_date_upper_range, insurance_type_id
过滤器
@filtered_quotes = current_user.insurance_subscribers.includes(:insurance_types).includes(:insurance_subscribers_types, :insurance_subscribers_types_carriers).
premium_amount(params[:dashboard][:premium_amount_lower_range], params[:dashboard][:premium_amount_upper_range]).
duration(params[:dashboard][:application_date_lower_range], params[:dashboard][:application_date_upper_range]).
insurance_type(params[:dashboard][:insurance_type_id])
But Now need to filter according to their status also. 但是现在还需要根据其状态进行过滤。 Having problem with it. 有问题。 I have status column in insurance_subscribers
and insurance_subscribers_types_carriers
tables, both columns are enum. 我在insurance_subscribers
和insurance_subscribers_types_carriers
表中有“状态”列,这两个列都是枚举。
I tried to add where clause like 我试图添加where子句,例如
@filtered_quotes = current_user.insurance_subscribers.includes(:insurance_types).includes(:insurance_subscribers_types, :insurance_subscribers_types_carriers).
where("insurance_subscribers_types_carriers.status = 1")
# ...
This is giving me error PG::UndefinedTable: ERROR: missing FROM-clause entry for table "insurance_subscribers_types_carriers"
这给了我错误PG::UndefinedTable: ERROR: missing FROM-clause entry for table "insurance_subscribers_types_carriers"
but when i try to do like 但是当我尝试去喜欢
@filtered_quotes = current_user.insurance_subscribers.includes(:insurance_types).includes(:insurance_subscribers_types, :insurance_subscribers_types_carriers).
where(status: 1)
# ....
This will put the where clause on insurance_subscribers. 这会将where子句放在insurance_subscribers上。
Trying to add a simple where clause WHERE insurance_subscribers_types_carriers.status = 1
in above query but Having so much trouble with this query. 试图在上述查询中添加一个简单的where子句WHERE insurance_subscribers_types_carriers.status = 1
,但此查询有很多麻烦。
Associations 协会
insurance_subscriber.rb insurance_subscriber.rb
has_many :insurance_subscribers_types, dependent: :destroy
has_many :insurance_types, through: :insurance_subscribers_types
has_many :insurance_subscribers_types_carriers, through: :insurance_subscribers_types
insurance_types.rb insurance_types.rb
has_many :insurance_subscribers, through: :insurance_subscribers_types
has_many :insurance_subscribers_types
has_many :insurance_subscribers_types_carriers
insurance_subscriber_type.rb insurance_subscriber_type.rb
belongs_to :insurance_subscriber
belongs_to :insurance_type
has_many :carriers, through: :insurance_subscribers_types_carriers
has_many :insurance_subscribers_types_carriers, dependent: :destroy
insurance_subscribers_types_carrier.rb insurance_subscribers_types_carrier.rb
belongs_to :carrier
belongs_to :insurance_subscribers_type
If you want to add queries
across associated models, first you need to join them. 如果要跨关联模型添加queries
,则首先需要将它们加入。 Since you have has_may :through
associations, you can accomplish want you want as follows: 由于您具有has_may :through
关联,因此可以按以下方式完成所需的操作:
InsuranceSubscriber.joins(insurance_subscriber_types: :insurance_subscribers_types_carriers)
.includes(:insurance_types, :insurance_subscribers_types, :insurance_subscribers_types_carriers)
.where("insurance_subscribers_types_carriers.status = ?", 1)
As you see, you can join and reference your associations, even you have has_many :through
associations as follows joins(.joins(insurance_subscriber_types: :insurance_subscribers_types_carriers)
. 如您所见,即使您具有has_many :through
,也可以加入并引用您的关联,如下所示joins(.joins(insurance_subscriber_types: :insurance_subscribers_types_carriers)
。
You will get sql an output as follows: 您将获得sql的输出,如下所示:
InsuranceSubscriber Load (3.3ms) SELECT "insurance_subscribers".* FROM
"insurance_subscribers" INNER JOIN "insurance_subscriber_types" ON
"insurance_subscriber_types"."insurance_subscriber_id" =
"insurance_subscribers"."id" INNER JOIN "insurance_subscribers_types_carriers" ON
"insurance_subscribers_types_carriers"."insurance_subscriber_type_id" =
"insurance_subscriber_types"."id" WHERE (insurance_subscribers_types_carriers.status = 1)
I replicated and tested your question with models structure, like these: 我使用模型结构复制并测试了您的问题,如下所示:
PS: I made small changes in your model names, so be careful. PS:我对您的型号名称做了一些小的更改,因此请小心。 They were so confusing try to simplify them. 他们是如此简化尝试。
insurance_subscriber.rb insurance_subscriber.rb
# == Schema Information
#
# Table name: insurance_subscribers
#
# id :integer not null, primary key
# name :string
# status :integer default("0")
# created_at :datetime not null
# updated_at :datetime not null
#
class InsuranceSubscriber < ActiveRecord::Base
has_many :insurance_subscriber_types, dependent: :destroy
has_many :insurance_types, through: :insurance_subscriber_types
has_many :insurance_subscribers_types_carriers, through: :insurance_subscriber_types
enum status: {active: 0, passive: 1}
end
insurance_subscriber_types.rb insurance_subscriber_types.rb
# == Schema Information
#
# Table name: insurance_subscriber_types
#
# id :integer not null, primary key
# name :string
# insurance_subscriber_id :integer
# insurance_type_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class InsuranceSubscriberType < ActiveRecord::Base
belongs_to :insurance_subscriber
belongs_to :insurance_type
has_many :insurance_subscribers_types_carriers, dependent: :destroy
has_many :carriers, through: :insurance_subscribers_types_carriers
end
insurance_subscribers_types_carriers.rb insurance_subscribers_types_carriers.rb
# == Schema Information
#
# Table name: insurance_subscribers_types_carriers
#
# id :integer not null, primary key
# carrier_id :integer
# insurance_subscriber_type_id :integer
# status :integer default("0")
# created_at :datetime not null
# updated_at :datetime not null
#
class InsuranceSubscribersTypesCarrier < ActiveRecord::Base
belongs_to :carrier
belongs_to :insurance_subscriber_type
enum status: {active: 0, passive: 1}
end
insurance_types.rb insurance_types.rb
# == Schema Information
#
# Table name: insurance_types
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#
class InsuranceType < ActiveRecord::Base
has_many :insurance_subscribers_types_carriers
has_many :insurance_subscribers_types_carriers
has_many :insurance_subscribers, through: :insurance_subscribers_types
end
If you want to use the included association (insurance_subscribers_types_carriers) in the query you have to add "references" otherwise insurance_subscribers_types_carriers will be loaded separately from the main query: 如果要在查询中使用包含的关联(insurance_subscribers_types_carriers),则必须添加“引用”,否则insurance_subscribers_types_carriers将与主查询分开加载:
InsuranceSubscriber.includes(:insurance_subscribers_types_carriers)
.where("insurance_subscribers_types_carriers.status = ?", 1)
.references(:insurance_subscribers_types_carriers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.