Blueprinter gem - render STI collection



我正在尝试使用序列化的新替代方案:Blueprinter

如果每个模型都是某种类型的视图,你如何处理STI集合?我想到了这样的东西(在AMS github问题上看到类似的代码):

def serialize_cards(cards)
cards.group_by(&:type).map do |type, cards|
case type
when 'TypeOneCard' then Api::V1::CardBlueprint.render(cards, view: :one)
when 'TypeTwoCard' then Api::V1::CardBlueprint.render(cards, view: :two)
else  raise ArgumentError, "Unhandled event type '#{type}'"
end
end
end

为什么输出为带反斜杠的字符串:

[
"[{"id":27566,"attachment":null,"avatar":null,"card_comments_count":0,"card_likes_count":0,"card_publisher":"group","created_at":"2020-11-18T10:24:42.195+01:00","description":"Ted, how many times have I told you to put the lid back on the peanut butter jar?! It’s this inconsiderate, immature jackassery that makes me feel like I’m living in The Real World House! And not the early days when they all had jobs and social consciences, I’m talking about Hawaii, and after!","ends_at":"2021-02-06T23:55:00.000+01:00","event_attendees_count":0,"group_name":"Cheeseburger - Herbes de Provence","origin":"http://localhost:3000/dorfplatz/3","origin_name":"Dardagny - Athenaz (Avusy)","picture":null,"starts_at":"2020-11-21T00:00:00.718+01:00","title":"The Doors","type":"EventCard","updated_at":"2020-11-19T10:25:49.467+01:00","user_full_name":"Carolann Ritchie"},{"id":28163,"attachment":null,"avatar":null,"card_comments_count":0,"card_likes_count":0,"card_publisher":"group","created_at":"2020-11-14T23:22:42.059+01:00","description":"That’s life, you know, we never end up where you thought you wanted to be.","ends_at":"2020-12-05T23:55:00.000+01:00","event_attendees_count":0,"group_name":"Cheeseburger - Herbes de Provence","origin":"http://localhost:3000/dorfplatz/3","origin_name":"Dardagny - Athenaz (Avusy)","picture":null,"starts_at":"2020-11-14T00:00:00.942+01:00","title":"Iron Maiden","type":"EventCard","updated_at":"2020-11-19T10:25:59.622+01:00","user_full_name":"Reuben Kertzmann"},{"id":24658,"attachment":null,"avatar":null,"card_comments_count":0,"card_likes_count":0,"card_publisher":"group","created_at":"2020-11-14T14:40:45.442+01:00","description":"You can’t just skip ahead to where you think your life should be.","ends_at":"2021-02-04T23:55:00.000+01:00","event_attendees_count":0,"group_name":"Cheeseburger - Herbes de Provence","origin":"http://localhost:3000/dorfplatz/3","origin_name":"Dardagny - Athenaz (Avusy)","picture":null,"starts_at":"2020-11-19T00:00:00.811+01:00","title":"Steely Dan","type":"EventCard","updated_at":"2020-11-19T10:24:58.452+01:00","user_full_name":"Reuben Kertzmann"}]",

使用render_to_hash代替将每个序列化器输出为JSON

def serialize_cards(cards)
cards.group_by(&:type).map do |type, cards|
case type
when 'TypeOneCard' 
Api::V1::CardBlueprint.render_to_hash(cards, view: :one)
when 'TypeTwoCard' 
Api::V1::CardBlueprint.render_to_hash(cards, view: :two)
else  
raise ArgumentError, "Unhandled event type '#{type}'"
end
end
end

相关内容

  • 没有找到相关文章

最新更新