Closed michaelirey closed 10 years ago
I have the same issue #513 with filtering, you can try
def include_name?; fasle end
My problem is the filter is overridden if the filtered key is used in the embed association.
Thanks for the tip, and glad to hear others are experiencing a similar issue.
I tried what you suggested, still does not get invoked.
@zhaohanweng, your suggestion does work. Still have broken filter though.
+1
filter not triggered for me on active_model_serializers (0.8.1) either. Ended up overriding attributes method instead (like in this rails cast: http://railscasts.com/episodes/409-active-model-serializers)
def attributes data = super data.delete(:name) data end
Doesn't work for me too. active_model_serializers 0.8.1.
The problem appears with active_model_serializers prior to version 0.9.0. I think that versions < 0.9.0 do not support the filter
function, at least considering that this method is not mentioned in the stable documentation
+1
@r4m, you're right, according to their changelog ( https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md ) filter
method was added in 0.9.0 pre1 release.
How stable is 0.9? Really want the filter method but a little nervous about upgrading given all the API changes.
+1
+1
+1
+1 here as well. Looks like 0.9 is in alpha, so I'm using serializable_hash
like this right now:
def serializable_hash
all = super
attrs_to_serialize = some_method
all.slice(*attrs_to_serialize)
end
(some_method
returns a set of attributes)
0.9.0 is coming out later today, and yes, it's not in 0.8.
Why is the filter feature not in the master Readme? It is described in branch 0-9-stable
, but it works in my project, which has simply gem 'active_model_serializers'
in the Gemfile (and 0.9.3
listed in Gemfile.lock). It took me way too long to find out about it. Many people are still posting super complicated solutions like using 3 separate serializers for one model.
As one how is/was searching... surprised not in README... guess the 10.x overshadowed it.
Hi @linesarefuzzy and @marknadig, indeed our efforts are deeply focused on 0.10.x, but we still want to help ppl who use older versions!
First of all, we strongly recommend you to check 0.10.x version and give it a try, we already have a RC2 and you can also point it to master if you like to live on the cutting-edge.
I've just checked the 0.9-stable branch and it seems the filter
features is mentioned there:
Also, as with attributes, serializers will execute a filter method to determine which associations should be included in the output. For example:
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
def filter(keys)
keys.delete :comments if object.comments_disabled?
keys
end
end
Or ...
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_one :author
has_many :comments
def filter(keys)
keys.delete :author unless scope.admin?
keys.delete :comments if object.comments_disabled?
keys
end
end
@joaomdmoura Yes, it's described in 0.9-stable but not in master. However, it works in whatever version is used when the Gemfile says gem 'active_model_serializers'
, which I assume is master or previous to master. Shouldn't it then also be in the master readme?
In my serializer
However, name attribute stays in.
I have tried this:
The pry is not invoked.