Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Association :through with :source ends up renaming the node #394

Open
jifka opened this issue Jan 24, 2013 · 0 comments
Open

Association :through with :source ends up renaming the node #394

jifka opened this issue Jan 24, 2013 · 0 comments

Comments

@jifka
Copy link

jifka commented Jan 24, 2013

In my model, I have Products and ProductUnits. A product has many ProductUnits associated with it. Since I need some attributes on the relationship, I've created a ProductComponent model which points to a single ProductUnit and associate Products to ProductUnits through the ProductComponent. Here are the associations in the Product class:

has_many :product_components
has_many :units, :through => :product_components, :source => :product_unit

This says that calling someprod.units returns a list of ProductUnit instances and prod.product_components returns a list of ProductComponent instances.

So in RABL, I defined a product like so...

object @prod
attributes :id, :active, :name, :description

child :units do
  attributes :id, :name
end

When I look at the resultant JSON, there's no .units array hanging off the product. So, what does it look like?

{
  id:1, active:true, name:"prodname", description:"this is a product",
  product_units:[
    {id:9, name:"some unit"}
  ]
}

Yes, an association :through, using a :source type, ends up rendering to the source's class name instead of what you've defined in the child node in RABL. But I found that using an alias works...but it looks funny:

child :units => :units do
  ...
end

This workaround....er...works, but it seems unnecessary. I'd think that the association name would be preserved. Am I thinking about this incorrectly?

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

No branches or pull requests

1 participant