Skip to content

Expose default value doesn't work for me #373

@j-boers-13

Description

@j-boers-13

Hi,

First of all thanks for the amazing work!
I am using grape entity to serialize ActiveRecord (Rails) objects to a hash, using .respresent(object)

My grape entity looks like this:

class Entities::ProductEntity < Grape::Entity
  expose :platform_id, as: :id
  expose :name
  expose :visible
  expose :main_image
  expose :url
  expose :short_description, default: ''
  expose :brand_id
  expose :category_ids
  expose :relevant_product_ids

  def brand_id
    object.brand&.platform_id if object.respond_to?(:brand)
  end

  def category_ids
    object.categories&.pluck(:platform_id) if object.respond_to?(:categories)
    []
  end

  def relevant_product_ids
    object.relevant_products&.pluck(:platform_id) if object.respond_to?(:relevant_products)
    []
  end
end

The default option on short_description doesn't work, neither did it work if i tried to use it in another entity with default: []

Am i doing something wrong? Or am I on an old version?

I am using https://github.com/ruby-grape/grape-entity#default-value this syntax, and am on grape-entity version 0.10.2

Hope you have some time to help me out, but no worries if not.

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions