0

I have following two Serializer class. In index controller I want skip loading project_products, only show/edit method I want to fetch project_product details.

class ProjectSerializer < ActiveModel::Serializer
      attributes :id, :name, :category, :project_category_id, :status, :description
      has_many :project_products
    end

class ProjectProductSerializer < ActiveModel::Serializer
  attributes :id, :name, :quantity
end

Controller:

  def index
    respond_with @projects
  end

def load_projects
    @projects = current_organization.projects.includes(:project_category)
  end
Santi
  • 620
  • 8
  • 22

2 Answers2

0

Try to override association methods

 class ProjectSerializer < ActiveModel::Serializer
      attributes :id, :name, :category, :project_category_id, :status, :description
      has_many :project_products

    def project_products
        if current_page?(edit_path_url)
            object.comments
        else
            object.comments = nil
    end

end
Defoncesko
  • 657
  • 5
  • 25
  • Thanks for your help but when i place the code `respond_with @projects.to_json(:except => ["project_products"])` it ignores my active serializer and showing all fields form projects table – Santi Nov 06 '15 at 11:36
  • That 's what you are using ? https://github.com/rails-api/active_model_serializers – Defoncesko Nov 06 '15 at 13:18
  • Did you try to override association methods and inside select what you need to return with conditions on path ? – Defoncesko Nov 06 '15 at 14:32
  • Simply i want to fetch some fields from project table when listing projects. And want product listing along with project details (not all fields) in project details page. obviously using activeModelSerializer – Santi Nov 06 '15 at 15:03
0

You can use the "except" param when creating the serializer instance:

respond_with @projects, except: project_products

(when the index controller action)

Tonatiuh
  • 2,205
  • 1
  • 21
  • 22