user2540748
user2540748

Reputation:

Active Model Serializer 0.10 filter on query params

Looking through the other questions, it seems that 0.10 no longer supports the old methods of filtering on query params.

class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name, :deals_count

  def deals_count
    byebug
    Deal.includes(:categories).where(categories: { id: @object.id }).count
  end

end

On the deals model, I have booleans for each day of the week (monday, tuesday, wednesday ... )

I want to be able to pass in (tuesday=true&, wednesday=true) etc as filters to add for this.

Upvotes: 0

Views: 640

Answers (1)

dpaluy
dpaluy

Reputation: 3705

You should use instance_options.

# controller
render json: @category, days: [1, 2, 4, 6, 7]

And your serializer should be something like:

class CategorySerializer < ActiveModel::Serializer
  attributes :id, :name, :deals_count

  def deals_count
    Deal.where(days: instance_options[:days])
      .includes(:categories).where(categories: { id: @object.id } ).count
  end
end

Note: any options passed to render that are not reserved for the adapter should be available

Upvotes: 1

Related Questions