سؤال

Similar to this question, I wonder how to customize Grape (build on top of Rails) to send all related IDs of a hasMany relation with the requested object, because Ember expect this format:

{ "customer": { "projects": [1, 2, 3] } }

Because Grape has his own serializer, I can't do something like this

class PostSerializer < ActiveModel::Serializer
  embed :ids

  attributes :id, :title, :body
  has_many :comments
end

Is there any simple solution like embed :ids or do I have to add the IDs manually?

هل كانت مفيدة؟

المحلول

Using grape entities, it'll be similar to that:

class CustomerEntity < Grape::Entity
  expose (:projects) { |entity| entity.projects.pluck(:id) }
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top