How to expose associated record using grape entity

870 views Asked by At

Hi I have a model Event and this event has one Setting. e.g

class Event < ActiveRecord::Base
  has_one :setting
end

class Setting < ActieRecord::Base
  belongs_to :event
end

My event entity looks some thing like

module Entities
    class EventEntity < Grape::Entity
      expose :id
      expose :name, documentation: { type: String, desc: "Event name", required: true}
      expose :setting, using: Entities::EventSettingEntity
    end
end

My EventSettingEntity class looks something like

module Entities
  class EventSettingEntity < Grape::Entity
    expose :id, :event_id
    expose :type, documentation: { type: String, desc: "Type" }
  end
end

My Api is like

desc 'Create an event.' ,{
        params: Entities::EventEntity.documentation, type: :create
}
post 'create' do
  event = Event.create(params)
  present event, with: EventEntity
end

But the main problem is event creation form is not showing setting class type field. Please help me to resolve the issue. Thanks

0

There are 0 answers