我有一個名爲featured_products的表只包含兩列(product_id,position)。 在我的GET/featured_products,我想呈現像這樣主動模型串行器,呈現無關鍵的關聯
[
{ "name":"Product 1" }
{ "name":"product 2" }
]
而是我在邏輯上得到這樣的:
[
"product":{ "name":"Product 1" }
"product":{ "name":"product 2" }
]
以下活性型號序列化的文檔,我心中已經嘗試了這些在我featured_product序列化器類:
embed_in_root: true
和
belongs_to :product, embed_in_root: true
但第一個給出錯誤,而第二個更改JSON決不會。
我不知道我是否錯過了我在Active Model Serializer的文檔中找到的答案,或者如果答案是在其他地方找到的,但我沒有設法自己解決這個問題,我會很高興在這裏得到一些建議。
謝謝
在這裏,它適合我的具體情況。出於好奇,是否還有將產品「包含」在我的JSON中,同時仍然在FeaturedProductSerializer中序列化它以包含其他東西(比如我的例子中的Position)? – Sonastra