Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:04:43+00:00 2026-06-09T14:04:43+00:00

How can I use a JSON service as a model for a Rails 3.2

  • 0

How can I use a JSON service as a model for a Rails 3.2 application?

I would like to link model methods to JSON requests, e.g.

  • MyModel.create to generate a JSON request to create a new record
  • MyModel.find to generate a JSON request to retrieve a record and decode the JSON response into a model object

What is the preferred way to build this type of functionality? One option that I am looking at is to create a custom DataMapper adapter.

Thanks.

Update:
Note that I can’t use ActiveResource because this requires the JSON service to respond in a certain way, which it doesn’t in this instance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:04:44+00:00Added an answer on June 9, 2026 at 2:04 pm

    Short answer: it depends on your JSON service.

    Is the service RESTful, for starters? If so, you’re in luck. ActiveResource is dead, so I wouldn’t advise using it, but the code itself would provide a good starting point for creating a set of wrapper methods like create and find to access your API and manipulate records the way ActiveRecord creates and queries records in the DB. That’s relatively easy if your API is RESTful because rails is built around RESTful-ness, so the mapping between the two becomes much cleaner.

    This article sums it up well:

    Rails makes it extremely easy to build Web services that follow the REST principles and work equally well with Web browsers and the programmable Web. In fact, a lot of that simplicity comes directly from following these principles. We didn’t have to tell our client how to create, read, update or delete the resource, those all followed from using the proper HTTP methods. All we had to do is point our client at the right place.

    If your service is not RESTful, which judging from the comments on other questions I think is perhaps the case, then you’ll have your work cut out for you. ActiveModel will not do this work for you: create is defined in ActiveRecord::Persistence, and find is defined in ActiveRecord::FinderMethods.
    They’re not in ActiveModel. ActiveResource is able to reproduce them fairly easily because it makes assumptions about the type of service it is interfacing with (i.e. that it is RESTful, plus a few other things).

    What ActiveModel offers is all the other stuff that makes rails so useful for dealing with models: its validation system, serialization methods, dirty tracking of attribute changes,
    callbacks (before_save, after_save, etc.), translation/localization, and so on. These are all very useful functions to have, but they still leave you with the problem of wrapping your API calls.

    So here’s what I would recommend, based on my albeit limited experience (see my final note about that):

    1. First, take a close look at your API and figure out how close it is to being RESTful. If it is not RESTful, you’ll have to think about how to work around this so you can treat it like a RESTful service. A good reference on how to do this is O’Reilly’s RESTful Web Services (specifically Chapter 2, “Writing Web Service Clients”.)
    2. Create an API wrapper around it that implements create, find and whatever other ActiveRecord-like functionality you want to have. You’ll probably want to use a gem like
      Faraday or HTTParty for actually making requests to the API rather than working directly with Net::HTTP. (I’ve never used DataMapper so can’t comment on that.)
    3. Include whatever elements of ActiveModel you want to use to make your wrapper class more like a rails model: validations, serialization etc. In Rails 4, you can actually include everything using the new barebones ActiveModel::Model.

    That will get you much of the way to using your JSON service as a Rails model.

    For the record, my experience in actually implementing this kind of thing is limited to building an API wrapper (in the works) for accessing an XML API. The wrapper has a class which includes ActiveModel validators for validating queries to the API. I’ve found the ActiveModel validators really useful for guaranteeing that the API is accessed correctly, but the wrapper is only for getting records from the API, not for actually creating or updating them, so the interface is quite a lot easier than what I expect you’ll be building.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain how to use JSON-RPC.net in my application for windows phone. When
I am getting JSON data from a web service and would like to display
Is there a JSON query class or jQuery plugin that I can use to
When working with JSON, I can use NuGet to add JSON.Net and this allows
I can use this maven plugin maven-jaxb-plugin to generate Java Classes from XSD file.
I can use .button() to create beautiful submit buttons, but the rest of the
I have an application in Asp.net MVC where at some point I would like
I would like to call a REST-service from a Black Widow 1.0 board (with
When trying to create a simple service to return a simple JSON string by
I know how to create a new backbone model. But how I can create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.