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

Wrong field order #57

Closed
rgorel opened this issue Jan 26, 2012 · 3 comments
Closed

Wrong field order #57

rgorel opened this issue Jan 26, 2012 · 3 comments

Comments

@rgorel
Copy link

rgorel commented Jan 26, 2012

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.

@fabrik42
Copy link
Owner

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

@rgorel
Copy link
Author

rgorel commented Jan 27, 2012

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

@fabrik42
Copy link
Owner

You're welcome! :)

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

2 participants