multiple has_one for the same entity but different record not getting side loaded #492

Closed
rahlawat opened this Issue Jan 21, 2014 · 2 comments

Projects

None yet

2 participants

@rahlawat

Hi all,

There seems a problem with side-loading has_one associations with nested models. Consider this example

class StudentSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :teachers, serializer: TeacherSerializer
  embed :ids, include: true
end

class TeacherSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_one :address, serializer: AddressSerializer
  embed :ids, include: true
end

class AddressSerializer < ActiveModel::Serializer
  attributes :id, :postcode
end

I need every association to be side loaded to work well with ember-data

Consider this scenario. Student has two teachers and each teacher has an address. So the addresses have to be side loaded. But in my case only one address is getting side-loaded. I should get a json output like this

{
 student: {
  id: 1,
  name: "rahlawat"
  teacher_ids: [1,2]
 }
 teachers: [
 {
   id: 1,
   name: "Teacher 1",
  address_id: 1
 },
 {
   id: 2,
   name: "Teacher 2",
   address_id: 2
  }],
 addresses: [
 {
   id: 1,
   postcode: "111"
  },
  {
   id: 2,
   postcode: "222"
  }]
}

But instead I am getting only the address with id 2.

@rahlawat

Do I need to provide more input to make the above problem statement more clear. The pull request provided by shakirshakiel might solve the problem I am facing.

@steveklabnik
Contributor

I merged #493, so I'm closing this. @rahlawat please let me know if it is still broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment