Skip to content

Conversation

jonah-williams
Copy link

See issue #366.

Previously has_many associations ignored the polymorphic: true option.

Now polymorphic has_many associations behave similarly to has_one associations. For included associations objects are grouped under root keys derived from their types rather than the association name.

For compatibility if a root key is specified on a embedded included polymorphic has_many association then all objects in that association will grouped under the specified root key instead of a key for each object type.

polymorphic has_many

has_many :edibles, polymorphic: true
{
  edibles: [
    {
      type: :orange,
      orange: {
        ...
      }
    }
    {
      type: :banana,
      banana: {
        ...
      }
    }
  ]
}

embedded polymorphic has_many

has_many :edibles, embed: :ids, polymorphic: true
{
  edibles: [
    {
      type: :orange,
      id: 1
    }
    {
      type: :banana,
      id: 1
    }
  ]
}

included embedded polymorphic has_many

has_many :edibles, embed: :ids, include: true, polymorphic: true
{
  meals: [{
    edibles: [
      {
        type: :orange,
        id: 1
      }
      {
        type: :banana,
        id: 1
      }
    ]
  }],
  oranges: [{
    id: 1,
    ...
  }],
  bananas: [{
    id: 1,
    ...
  }]
}

Jonah Williams added 5 commits July 29, 2013 17:02
Need to test cases with polymorphic associations containing multiple types and where the association name does not match the name of the contained types.
Polymorphic has_many associations using `include: true` include their items under keys matching the type names of each item.
Separate embedded non-included has_one and has_many polymorphic association tests.
@coveralls
Copy link

Coverage Status

Coverage increased (+0%) when pulling 1038ae1 on carbonfive:has_many_support_polymorphic_associations into 8ac4bf9 on rails-api:master.

@spastorino
Copy link
Contributor

Polymorphic associations are not any longer on master and we would need to reimplement them before releasing 0.9. Support for has_many should be added also. If some of you want to take a look at that, please go ahead.
I'm closing this for now.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants