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

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

Closed
batter opened this issue Nov 4, 2013 · 5 comments
Closed

Comments

@batter
Copy link

batter commented Nov 4, 2013

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!

nesquena added a commit that referenced this issue Dec 6, 2013
@nesquena
Copy link
Owner

nesquena commented Dec 6, 2013

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!

@phuongnd08
Copy link

@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?

@nesquena
Copy link
Owner

Maybe try this?

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

@phuongnd08
Copy link

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

@nesquena
Copy link
Owner

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.

DouweM added a commit to DouweM/rabl that referenced this issue Oct 8, 2014
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

3 participants