如何构建 Rails ActiveRecord 查询以对 belong_to 其他模型的对象进行分组?
How to get build Rails ActiveRecord query to group objects that belong_to other model?
给定以下模型:
ServiceCenter
has_many :country_codes
CountryCode
belongs_to :service_center
ActiveRecord 查询是什么 return 像这样:
{
"US": ["United States"],
"PT": ["Portugal", "Spain", "Estonia"],
"FR": ["France", "Germany", "Austria"]
}
其中键是每个ServiceCenter的country
属性,值是属于它的每个CountryCode的country_name
属性?在其他换句话说,我只想要一个属于每个 ServiceCenter 的国家代码列表,只显示那些属性。
{ 'service_centers.country': 'country_code.country_name', 'country_code.country_name' }
我试过这个:
CountryCode
.joins(:service_center)
.select('country_codes.country_name', 'service_centers.country')
.group('service_centers.country')
但是这个 returns:
<ActiveRecord::Relation [
<CountryCode id: nil, country_name: "Portugal">,
<CountryCode id: nil, country_name: "United States">,
<CountryCode id: nil, country_name: "Portugal">.....]>
我也试过 ServiceCenter.joins(:country_code)....
但结果相似 - ActiveRecord 与 ServiceCenter 对象的关系,其 ID 为零,并且给出了 country
属性。
我看过与此类似的答案:Get all records grouped by field from association and sorted by count in group,但我不想计数。
如有任何帮助,我将不胜感激!
不建议获取如下所有记录,因为它会有优化问题。但是,为了您的理解,您可以尝试:
hash = {}
ServiceCenter.all.each do |service_center|
hash[service_center.country] = service_center.country_codes.pluck(:country_name)
end
输出 hash
就像:
{
"US": ["United States"],
"PT": ["Portugal", "Spain", "Estonia"],
"FR": ["France", "Germany", "Austria"]
}
注意:Hash不能有你指定的多个值,应该是Array的形式。
编辑
不完全是你想要的,但这可能有点帮助:
ServiceCenter.joins(:country_codes).group("service_center_id").pluck("service_centers.country, GROUP_CONCAT(country_codes.country_name)")
输出
[["US", "United States"], ["PT", "Portugal, Spain, Estonia"], ["FR", "France, Germany, Austria"]]
给定以下模型:
ServiceCenter
has_many :country_codes
CountryCode
belongs_to :service_center
ActiveRecord 查询是什么 return 像这样:
{
"US": ["United States"],
"PT": ["Portugal", "Spain", "Estonia"],
"FR": ["France", "Germany", "Austria"]
}
其中键是每个ServiceCenter的country
属性,值是属于它的每个CountryCode的country_name
属性?在其他换句话说,我只想要一个属于每个 ServiceCenter 的国家代码列表,只显示那些属性。
{ 'service_centers.country': 'country_code.country_name', 'country_code.country_name' }
我试过这个:
CountryCode
.joins(:service_center)
.select('country_codes.country_name', 'service_centers.country')
.group('service_centers.country')
但是这个 returns:
<ActiveRecord::Relation [
<CountryCode id: nil, country_name: "Portugal">,
<CountryCode id: nil, country_name: "United States">,
<CountryCode id: nil, country_name: "Portugal">.....]>
我也试过 ServiceCenter.joins(:country_code)....
但结果相似 - ActiveRecord 与 ServiceCenter 对象的关系,其 ID 为零,并且给出了 country
属性。
我看过与此类似的答案:Get all records grouped by field from association and sorted by count in group,但我不想计数。
如有任何帮助,我将不胜感激!
不建议获取如下所有记录,因为它会有优化问题。但是,为了您的理解,您可以尝试:
hash = {}
ServiceCenter.all.each do |service_center|
hash[service_center.country] = service_center.country_codes.pluck(:country_name)
end
输出 hash
就像:
{
"US": ["United States"],
"PT": ["Portugal", "Spain", "Estonia"],
"FR": ["France", "Germany", "Austria"]
}
注意:Hash不能有你指定的多个值,应该是Array的形式。
编辑
不完全是你想要的,但这可能有点帮助:
ServiceCenter.joins(:country_codes).group("service_center_id").pluck("service_centers.country, GROUP_CONCAT(country_codes.country_name)")
输出
[["US", "United States"], ["PT", "Portugal, Spain, Estonia"], ["FR", "France, Germany, Austria"]]