JSON attributes of the superclass are not rendered
Opened this issue · 2 comments
I created a fresh project using your gem to create an inheritance model. I scaffolded two basic models:
Shape: area:integer, color:string # acts_as_superclass
Circle: radius:integer # inherits_from :shape
I changed the migrations and classes according to the README. Further, I seeded some test data:
Shape.create!(area: 33, color: "orange")
Circle.create!(area: 142, color: "dark green", radius: 4)
I would like to use the JSON version of the data. Therefore, I call their resource path:
http://localhost:3000/circles.json
http://localhost:3000/shapes.json
But what I get is not what I expect. Circle
objects are render with the following attributes only: shape_id
, radius
, created_at
, updated_at
. Where are the properties of the superclass?
Update: Shape
objects do not appear at all?!
Here's how I solved this problem. Not sure if it's the "right" way, the "easiest" way or the most "rails-esque" way.
In my model (In this case, Circle
), I overloaded the as_json()
method like this:
def as_json (options={})
super(:methods => [:area, :color])
end
Of course, that's a simplification, but hopefully it can point you in the right direction.
@mitzimoto I consider this as a workaround. However, I do not think I can use it in the final version. I am planning to use a JSON builder such as RABL or Boxer to handle more complex models. Such a custom change might interfer with their integration.
Also, you probably agree on that you do not want to manually edit the list of attributes in the longterm.