我的观点中有以下Rabl:
node(:relations) do |p|
related = p.relations.pluck(:related_to_id)
Spree::Product.find(related)
end
这将呈现以下内容:
"relations": [
{
"product": {
"id": 2,
"name": "T-SHIRT",
"description": "Awesome T shirts"
"created_at": "..."
"updated_at: "..."
.
.
.
bunch of other columns that I don't need.
我的问题是如何只抓取:name
和:description
,以便JSON输出如下所示:
"relations": [
{
"product": {
"name": "T-SHIRT",
"description": "Awesome T shirts"
}
]
我已尝试映射它,Spree::Product.find(related).map { |r| [r.name, r.description] }
但是只返回值,如下所示:
"relations": [
"T-SHIRT",
"Awesome T shirts"
]
感谢您的帮助!
更新:
当我写:
child :related_products do
attributes :name, :description
end
我明白了:
"spree_relations": [
{}
]
链接到模型:
https://github.com/spree-contrib/spree_related_products/blob/master/app/models/spree/relation.rb
答案 0 :(得分:1)
嗯,可能有多种方法可以做到。
您可以使用rails #as_json
方法。
node(:relations) do |p|
related = p.relations.pluck(:related_to_id)
Spree::Product.find(related).as_json(only: [:name, :description])
end
或者你可以试着用拉布的方式做。
child :related_products do
attributes :name, :description
end
但为此您可能需要更改模型。