0

I have a rails app with the following models.

class Project
  has_many :project_clips
  has_many :clips, through: :project_clips
end

class Clip
  has_many :project_clips
  has_many :projects, through: :project_clips.
end

class ProjectSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :clips
end

class ClipSerializer < ActiveModel::Serializer
  attributes :id, :name
end

I was wondering if it's possible to display the values of the associated project_clip, if the clip has been called within the context of project. Let's say the ProjectClip model, has a field called priority. I want the results to show up like this.

{ projects: { "id": 1, "name": "ipsum", "clips": [{ "id": 1, "name": "lorem", "priority": "high"}] } }

I don't want the values of project_clips to be included, just a few properties when returning the data for projects.

ShivamD
  • 931
  • 10
  • 21

1 Answers1

0

If I'm getting your question right, you can do something like:

res = project.clips.collect{ |clip| [clip, clip.project_clips] }

or if you want to return hashes and not objects, you can do:

res = project.clips.collect{ |clip| [clip.attributes, clip.project_clips.collect{|pc| pc.attributes}] }
shivam
  • 16,048
  • 3
  • 56
  • 71