0

I have a grouped collection and I want to serialize it but it returns me with an error 'undefined method `read_attribute_for_serialization' for # '. This is my sample data without serializer:

{
    "message": "success",
    "data": {
        "reports": {
            "export": [
                {
                    "name": "Order",
                    "report_type": "export",
                    "parameters": "\"{order_date: '10-10-2019'}\"",

                },
                {
                    "name": "Order",
                    "report_type": "export",
                    "parameters": "\"{order_date: '10-10-2019'}\"",
                }],
            "import": [
                {
                    "name": "Order",
                    "report_type": "import",
                    "parameters": "\"{order_date: '10-10-2019'}\"",
                },
                {
                    "name": "Order",
                    "report_type": "import",
                    "parameters": "\"{order_date: '10-10-2019'}\"",
                }
            ],
            "details": [
                {
                    "name": "Order",
                    "report_type": "details",
                    "parameters": "\"{order_date: '10-10-2019'}\"",
                },
                {
                    "name": "Order",
                    "report_type": "details",
                    "parameters": "\"{order_date: '10-10-2019'}\"",
                }
            ]
        }
    }
}

This is my serializer:


class ReportSerializer < ActiveModel::Serializer
  attributes :id, :name, :report_type, :parameters

  def parameters
    JSON.parse(object.parameters)
  end
end

This is my controller code:

  def reports_list
    reports = Report.all.group_by(&:report_type)
    render_collection(reports, { name: 'reports' }, each_serializer: ReportSerializer)
  end

But return me with an error : undefined method `read_attribute_for_serialization' for #<Hash:0x0000562eebbddee8>

Please help. Thanks.

2
  • The parameters has is quoted with double quotes around it. You need to remove quotes. Commented Apr 30, 2020 at 11:46
  • @AmitPatel Yes but that isn't related to the error she is getting. Commented Apr 30, 2020 at 16:45

1 Answer 1

2

*.group_by() is an Enumerable method which returns Hash - I think render_collection expects to receive ActiveRecord::Relation - use *.group(:report_type) instead

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.