Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to filter attributes? #992

Closed
Yoshyn opened this issue Jul 8, 2015 · 3 comments
Closed

How to filter attributes? #992

Yoshyn opened this issue Jul 8, 2015 · 3 comments

Comments

@Yoshyn
Copy link

Yoshyn commented Jul 8, 2015

Hi, I want to be able to filter over attributes like this :

   render json: @model, fields: [:fields1, :fields2]

The fields options can be found here : https://github.com/rails-api/active_model_serializers/blob/master/lib/action_controller/serialization.rb

   ADAPTER_OPTION_KEYS = [:include, :fields, :adapter]

But this does not work. I try to understand a few the code and write something like this and binding inside the attributes method :

ActiveModel::Serializer::Adapter::JsonApi.new(MyModelSerializer.new(Model.find(X), fields: [:id])).serializable_hash(fields: [:fields1])

Let's see the attributes method inside serializer:

    def attributes(options = {})
      attributes =
        if options[:fields] #options is nil, @options is {:fields=>[:fields1]}
          self.class._attributes & options[:fields]
        else
          self.class._attributes.dup
        end
    ....
    end

I don't understand where i have to pass the fields options in order to the attributes method get it in these params method.
Why there's an options parameter on the attributes method. Why not use the @options var?

@joaomdmoura
Copy link
Member

Hey @Yoshyn,

Indeed, fields seems to be only used on JSON API adapter, like included.

You can check the PR that implemented it here

I've just opened a new PR (#999) that fix some options mistakes and implements the fields option on Json Adapter.

@joaomdmoura
Copy link
Member

@Yoshyn, have you checked #999 already? Wanted tome 👍 before thinking about merge it.

@joaomdmoura
Copy link
Member

closing this one in favor of #1058

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants