質問

I'm using active_model_serializers and would like to find a way to include all model's attributes by default and then to use something like this

       exclude :date_created, :first_name

to specify the ones that I don't need.

Until now I didn't find a way to specify the exported attributes besides the one in the docs and that is done by enumerating all of the needed attributes:

       attributes :title, :body
役に立ちましたか?

解決

You could do something like this on your model serializer (taking an example of User as the model):

class UserSerializer < ApplicationModelSerializer
   attributes(*User.attribute_names.map(&:to_sym))
end

More information about ActiveRecord attribute_names can be found here: http://apidock.com/rails/ActiveRecord/AttributeMethods/attribute_names

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top