Skip to content

ActiveResource::Base + ActiveModel::Serializer in rails3 vs. rails4 #102

Closed
redrick opened this Issue Oct 9, 2013 · 7 comments

4 participants

@redrick
redrick commented Oct 9, 2013

Hi guys,

I hope the title isn't somehow misleading, if so say and I will change it for you :)

but I am standing in front of quiet a problem now...
I got rails 3.2.14 app from my coworkers and they asked me to migrate it to 4.0.0, so i did and hence the activeresource is gem now I added

gem 'activeresource', require: 'activeresource'

and all went well, app is behaving normally, except this case:
we got company model as activeresource model and it is getting this data from service running on rails 4.0.0, which returns nicely formatted data of company (did through action_model_serializers in the service app), but all data and I does not want to show it all to user, but internally in app I need all those...
so of course in the main app I got company data as activeresource model, and here I got another serializer to serialize my output and after that I return nice json (I forgot to mention, that the main app is REST API)
this works nicely with rails 3.2.14 (just the main app, the service was on rails 4.0.0 for a long time) and if I get bundle list in rails 3.2.14 i see the gem version used

* activeresource (3.2.14)

but then after updating to rails 4.0.0, i got the answer from the service the same way, but this time the serializer is completely ignored in the main app and returns my data without any warning or error in the format I got from the service, which is painfully wrong :(

have you guys had problems like this or is this wanted behaviour, I have to say that I have no clue what to do with it now... :(

I am sorry if I gave you bad description, if needed I will try to make demo apps for this case

every answer will be highly appreciated, thx :)

UPDATE:

so to prove my point here, I got myself setup two apps as the ones I was describing
first one is the service (just provides /companies for the main_app)
pretty simple to get it running, just:

$ bundle install
$ rake db:migrate
$ rake db:seed
$ rails s -p 3010

this one is backed by DB, so you have to migrate that and i got my main app setup to looks for it on port 3010, so thats for the rails s

and then there is the main_app which got two branches, one is using rails 3.2.14 (branch rails3) and the other one is using rails 4.0.0 (branch rails4)
so you can switch accordingly and now to get it running, just :

$ bundle install
$rails s

and you got it going on version of rails you deserve, and my point here is that the rails4 one completly ignores serializer in main_app and returns data in other format than before...

but those app rely on the format of data and it is a big big problem now...
ex. rails3 output, which is the deserved one looks like this:

{
  companies: [
    {
      name: "HelloWorld s.r.o",
      tax_number: "CZ3423432490035"
    },
    {
      name: "MyExample a.s.",
      tax_number: "CZ342378979435"
    },
    {
      name: "HelloWorld s.r.o",
      tax_number: "CZ34353324345"
    },
    {
      name: "HelloWorld s.r.o",
      tax_number: "CZ342354654435"
    }
  ]
}

but when you switch to rails4 you got this:

[
  {
    id: 1,
    name: "HelloWorld s.r.o",
    tax_number: "CZ3423432490035",
    address: "Brno, Jostova 21, 60200"
  },
  {
    id: 2,
    name: "MyExample a.s.",
    tax_number: "CZ342378979435",
    address: "Brno, Jostova 22, 60200"
  },
  {
    id: 3,
    name: "HelloWorld s.r.o",
    tax_number: "CZ34353324345",
    address: "Brno, Jostova 23, 60200"
  },
  {
    id: 4,
    name: "HelloWorld s.r.o",
    tax_number: "CZ342354654435",
    address: "Brno, Jostova 24, 60200"
  }
]

which throws the whole app into confusion :)

I hope you know what I am talking about now and will help me to solve the issue
As I look at it, it may be the rails mistake, I am not sure, please say if its just some configuration I overlooked or something, thx for your time guys :)

@redrick
redrick commented Nov 11, 2013

ping :star2:

guys is activeresource still alive?

@josh-h
josh-h commented Dec 19, 2013

I've just run into the same issue, what was your solution?

@redrick
redrick commented Dec 19, 2013

oh snap, I merged the service with app (I actually use more services, but don't have issues with those other)

I thought nobody uses activeresource anymore, maybe some guys will have look at it soon... now that its not only me having this problem... hmmm

(merging was not a good move for me, but dont have time to solve this :( )

@ches
ches commented Feb 21, 2014

There is a setting which you'll find in both Rails and ActiveResource called include_root_in_json -- look into that and it should answer most of the questions here. At some point in the Rails 3 timeline, core changed their mind on the default, from true to false. For a long time as a deprecation measure, Rails 3 would generate an initializer in new apps at config/initializers/wrap_parameters.rb which set the value to false.

In ActiveResource you will also find ActiveResource::Base.include_root_in_json and MyModelClass.include_root_in_json.

If you're using ActiveModel Serializers on either side, you'll want to look at it's configuration for root elements.

@tamaloa
tamaloa commented Mar 10, 2014

Maybe it would be a good idea to put some information on this more prominently in the README. I just got bitten by a similar problem. My activeresource backed model was reporting to a rails 4 app. After updating from activeresource 3.2.17 to 4.0.0 everything was fine except once in a while saving the activeresource model failed (server 500). After some research i found out it was a single attributes fault.

The activeresource backed model had a attribute named the same as the class itself. Thus i suppose rails 4 has some kind of magic in place in where it looks for a key in params with the object's name and if it is not found it uses the complete params hash as if it was the model. If the params hash contained the attribute with the same name as the class this obviously had to fail :/

This repo shows my confusion :)

@redrick
redrick commented Mar 11, 2014

@ches I am sorry for what seemed as total ignore of your answer, but that actually is the solution :)

how could have I been so blind...

Everybody I encourage you as @ches said to look at these things that are already there in README...

Sorry for unnecessary issue report and thank you guys, I knew you would help :+1:

@redrick redrick closed this Mar 11, 2014
@ches
ches commented Mar 11, 2014

No worries, glad it was helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.