nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support

Home Page:http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change with the way associations are handled with STI that changed in v0.9.0

batter opened this issue · comments

I'm not sure what the actual change was that made this occur, as I didn't see anything in the CHANGELOG that indicated that this behavior should have even happened, so I wanted to mention it here in the issue log so that the author was aware.

I have a table using Single Table Inheritance (STI) in my Rails project, and the way that the label of the 'root' object for the associations between the objects changed in a way that broke our API after upgrading from RABL 0.8.6 to 0.9.0.

Here is the ActiveRecord setup:

# Parent class
# == Schema Information
#
# Table name: nodes
#
#  id                  :integer          not null, primary key
#  title               :string(255)
#  description         :text
#  pos                 :integer
#  type                :string(255)
#  parent_id           :integer
class Node < ActiveRecord::Base
end

class Unit < Node
  has_many :sections, :foreign_key => :parent_id, :inverse_of => :parent, :dependent => :destroy
  alias_method :children, :sections
end

class Section < Node
  belongs_to :parent, :class_name => 'Unit', :inverse_of => :sections
end

Here is the setup for the RABL view template:

# views/api/unit.json.rabl
object @unit
attributes :id, :type, :pos, :title, :description
child(:sections) do
  attributes :id, :type, :pos, :title, :description
end

With RABL 0.8.6, the output would look like this:

{
  id: 1,
  type: 'Unit',
  pos: 1,
  title: 'A Unit',
  description: 'Sample Desc.',
  sections: [
    {
       id: 2,
       type: 'Section',
       pos: 1,
       title: 'A Section',
       description: 'blah',
     }
  ]
}

After upgrading RABL to 0.9.0, the output changed so that the label for the root node of the sections group, was now labeled 'nodes' instead of 'sections' like so:

{
  id: 1,
  type: 'Unit',
  pos: 1,
  title: 'A Unit',
  description: 'Sample Desc.',
  nodes: [
    {
       id: 2,
       type: 'Section',
       pos: 1,
       title: 'A Section',
       description: 'blah',
     }
  ]
}

We made no change to the RABL view template, so this is a change in the way the gem behaves when handling associations that have STI like the above example. I was able to get it to revert to the 0.8.6 behavior by changing the line in the view file from this:

child(:sections) do

to this:

child(:sections => :sections) do

Not a huge deal at the end of the day, but because there didn't seem to be anything mentioning this change in behavior on the CHANGELOG, ran into it unexpectedly and it briefly broke our API while we figured out what was going on here. Not sure if this behavior change was intentional or not, but I figured I should mention it here so you could either list this in your 'Breaking Changes' section on the README or at least in the CHANGELOG. Great gem btw!

Added a note about this change. As far as I know this wasn't necessarily an intentional change so I apologize about it breaking your API. Hopefully the note in the README helps the next person. Thanks for heads up!

@nesquena So I have this:

collection @cards
attributes :type
child(:fact, if: lambda { |c| c.type == "fact" }, root: false) do
  attributes :image_url, :text
end

But fact is an instance of InterestingFact, so I get: { interesting_fact: { image_url: '...', text: '...' } }

If i changed to

child(:fact => :fact, if: lambda { |c| c.type == "fact" }, root: false) do
  attributes :image_url, :text
end

I get: Failed to render missing attribute image_url

Please advice?

Maybe try this?

child({ :fact => :fact }, if: lambda { |c| c.type == "fact" }, root: false) do
  attributes :image_url, :text
end

I see, so wrap it in a hash so rabl won't be confused between the field name and the if statement, right?

That's exactly right. Admittedly confusing that it doesn't work the other way but that should fix it. 

On Sun, Mar 30, 2014 at 8:59 PM, Phuong Nguyen <notifications@github.com="mailto:notifications@github.com">> wrote:

I see, so wrap it in a hash so rabl won't be confused between the field name and the if statement, right?


Reply to this email directly or view it on GitHub.