ActiveModel::Serializersを使用して API を構築しています。paramsを使用して条件付きでデータをサイドロードする最良の方法は何ですか?
だから私は次のようなリクエストをすることができますGET /api/customers
:
"customers": {
"first_name": "Bill",
"last_name": "Gates"
}
とGET /api/customers?embed=address,note
"customers": {
"first_name": "Bill",
"last_name": "Gates"
},
"address: {
"street": "abc"
},
"note": {
"body": "Banned"
}
パラメータによっては、そのようなもの。include_[ASSOCIATION]?
ActiveModel::Serializers に構文があることは知っていますが、コントローラーから効率的に使用するにはどうすればよいですか?
これは私の現在の解決策ですが、きちんとしていません:
customer_serializer.rb:
def include_address?
!options[:embed].nil? && options[:embed].include?(:address)
end
application_controller.rb:
def embed_resources(resources = [])
params[:embed].split(',').map { |x| resources << x.to_sym } if params[:embed]
resources
end
customers_controller.rb:
def show
respond_with @customer, embed: embed_resources
end
もっと簡単な方法である必要がありますか?