fabrik42/acts_as_api

Wrong field order

rgorel opened this issue · 3 comments

Hello,

First of all many thanks for your awesome gem, it could save a lot of my time if only I could find it earlier :)

So the problem is that my model fields are ordered wrong in JSON or XML output.

I have this in my model:

  api_accessible :full_profile do |t|
    t.add :id
    t.add :username
    t.add :status
    t.add :text_status
    t.add :status_updated_at
    t.add lambda {|user| user.picture.url(:medium)}, :as => :picture
    t.add :age
    t.add :bio
    t.add :lat
    t.add :long
    t.add :registered_time_ago
  end

And this is what I have in XML output:

<user>
  <status type="integer">1</status>
  <picture>/system/pictures/1/medium/various_human_21.jpg</picture>
  <age nil="true"></age>
  <lat>39.7756093397402</lat>
  <long>-73.1707363222342</long>
  <text_status nil="true"></text_status>
  <username>user1</username>
  <bio nil="true"></bio>
  <id type="integer">1</id>
  <status_updated_at nil="true"></status_updated_at>
  <registered_time_ago>1 day</registered_time_ago>
</user>

I am writing a web service for iOS app, and the iOS developer says that each time I deploy the code the fields are ordered in some random way, and it messes things up. The strict order is important for the XML parsing library that he uses.

Is there any solutions for this issue?

Thanks.

Hey,

at the moment there is no solution in acts_as_api, I guess.

@yourmodel.as_api_response(:full_profile)

gives you the hash that acts_as_api passes to the Rails XML renderer. Is this still in the correct order?

Also: Which Ruby version are you using? The behaviour of the order in a Hash differs from 1.8 to 1.9: http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/

Let me know what you find out :)

Best,

Chris

I was using 1.8.7. Upgrading to 1.9.2 solved the problem. Thanks a lot.

However, switching to 1.9 for a large projects could be very expensive, so please think about using this: http://api.rubyonrails.org/classes/ActiveSupport/OrderedHash.html

You're welcome! :)