Mongoid 将结果聚合到 rails 模型的实例中
Mongoid Aggregate result into an instance of a rails model
简介
更正遗留代码,有一个对象 LandingPage
的索引,其中大多数列应该是可排序的,但实际上不是。这大部分已更正,但很少有专栏继续给我带来麻烦。
这些列是需要聚合的列,因为基于其他文档的计数。为了简化问题的解释,我将只讨论其中一个称为 Visit
,因为其余代码只是重复。
代码获取排序和分页数据,然后在发送回 json 之前使用 LandingPage
方法修改每个对象。已经这样了,修改不了
因此,我需要进行聚合(按 Visit
计数对 LandingPage
进行排序),然后将对象获取为 LandingPage
实例以让遗留代码继续工作他们。
问题是无法将 Mongoid::Document 转换为 LandingPage 实例
这是我得到的错误:
Mongoid::Errors::UnknownAttribute:
Message:
unknown_attribute : message
Summary:
unknown_attribute : summary
Resolution:
unknown_attribute : resolution
这是我的代码:
def controller_function
landing_pages = fetch_landing_page
landing_page_hash[:data] = landing_pages.map do |landing_page|
landing_page.do_something
# Do other things
end
render json: landing_page_hash
end
def fetch_landing_page
criteria = LandingPage.where(archived: false)
columns_name = params[:columns_name]
column_direction = params[:column_direction]
case order_column_name
when 'visit'
order_by_visits(criteria, column_direction)
else
criteria.order_by(columns_name => column_direction).paginate(
per_page: params[:length],
page: (params[:start].to_i / params[:length].to_i) + 1
)
end
def order_by_visit(criteria, order_direction)
def order_by_visits(landing_pages, column_direction)
LandingPage.collection.aggregate([
{ '$match': landing_pages.selector },
{ '$lookup': {
from: 'visits',
localField: '_id',
foreignField: 'landing_page_id',
as: 'visits'
}},
{ '$addFields': { 'visits_count': { '$size': '$visits' }}},
{ '$sort': { 'visits_count': column_direction == 'asc' ? 1 : -1 }},
{ '$unset': ['visits', 'visits_count'] },
{ '$skip': params[:start].to_i },
{ '$limit': params[:length].to_i }
]).map { |attrs| LandingPage.new(attrs) { |o| o.new_record = false } }
end
end
我试过的
- 将控制台中的散列复制并粘贴到
LandingPage.new(attributes)
,实例已创建且有效。
- 将 attributes 键从 string 更改为 symbole,但仍然没有用。
- 对 returned 数组的任何元素使用
is_a?(hash)
return 正确。
- 把它放到json,然后再返回一个散列。仍然有 Mongoid::Document.
如何使聚合的 return 成为 LandingPage 的有效实例?
聚合管道由 Ruby MongoDB 驱动程序实现,而不是由 Mongoid 实现,因此 return Mongoid 模型实例。
documentation 中给出了如何获取 Mongoid 模型实例的示例。
简介
更正遗留代码,有一个对象 LandingPage
的索引,其中大多数列应该是可排序的,但实际上不是。这大部分已更正,但很少有专栏继续给我带来麻烦。
这些列是需要聚合的列,因为基于其他文档的计数。为了简化问题的解释,我将只讨论其中一个称为 Visit
,因为其余代码只是重复。
代码获取排序和分页数据,然后在发送回 json 之前使用 LandingPage
方法修改每个对象。已经这样了,修改不了
因此,我需要进行聚合(按 Visit
计数对 LandingPage
进行排序),然后将对象获取为 LandingPage
实例以让遗留代码继续工作他们。
问题是无法将 Mongoid::Document 转换为 LandingPage 实例
这是我得到的错误:
Mongoid::Errors::UnknownAttribute:
Message:
unknown_attribute : message
Summary:
unknown_attribute : summary
Resolution:
unknown_attribute : resolution
这是我的代码:
def controller_function
landing_pages = fetch_landing_page
landing_page_hash[:data] = landing_pages.map do |landing_page|
landing_page.do_something
# Do other things
end
render json: landing_page_hash
end
def fetch_landing_page
criteria = LandingPage.where(archived: false)
columns_name = params[:columns_name]
column_direction = params[:column_direction]
case order_column_name
when 'visit'
order_by_visits(criteria, column_direction)
else
criteria.order_by(columns_name => column_direction).paginate(
per_page: params[:length],
page: (params[:start].to_i / params[:length].to_i) + 1
)
end
def order_by_visit(criteria, order_direction)
def order_by_visits(landing_pages, column_direction)
LandingPage.collection.aggregate([
{ '$match': landing_pages.selector },
{ '$lookup': {
from: 'visits',
localField: '_id',
foreignField: 'landing_page_id',
as: 'visits'
}},
{ '$addFields': { 'visits_count': { '$size': '$visits' }}},
{ '$sort': { 'visits_count': column_direction == 'asc' ? 1 : -1 }},
{ '$unset': ['visits', 'visits_count'] },
{ '$skip': params[:start].to_i },
{ '$limit': params[:length].to_i }
]).map { |attrs| LandingPage.new(attrs) { |o| o.new_record = false } }
end
end
我试过的
- 将控制台中的散列复制并粘贴到
LandingPage.new(attributes)
,实例已创建且有效。 - 将 attributes 键从 string 更改为 symbole,但仍然没有用。
- 对 returned 数组的任何元素使用
is_a?(hash)
return 正确。 - 把它放到json,然后再返回一个散列。仍然有 Mongoid::Document.
如何使聚合的 return 成为 LandingPage 的有效实例?
聚合管道由 Ruby MongoDB 驱动程序实现,而不是由 Mongoid 实现,因此 return Mongoid 模型实例。
documentation 中给出了如何获取 Mongoid 模型实例的示例。