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 6167809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:34:33+00:00 2026-05-23T22:34:33+00:00

I am currently in the process of designing a versioned API for a new

  • 0

I am currently in the process of designing a versioned API for a new website. I understand how to namespace the routes but I am stuck on the best way to implement versioned methods within a model.

The code samples below are using the rails framework but the principle of the matter should be consistent between most web frameworks.

The routes currently look something like:

MyApp::Application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :products, :only => [:index, :show]
    end
  end
end

And the controller:

class Api::V1::ProductsController < V1Controller
  respond_to :json, :xml

  def index
    respond_with @products = Product.scoped
  end

  def show
    respond_with @product = Product.find(params[:id])
  end
end

So obviously we’re just exposing the attributes available on Product here, this solution works great if you’re only going to have one version of the API. What happens when you want to release V2 and V2 needs to reimplement the way that Product’s name is displayed (while maintaining backwards compatibility with V1 – at least in the short term)?

As far as I see it you have a couple of options…

  1. Drop support for V1 immediately and deal with the fallout (worst possible solution)
  2. You start overriding the to_[format] methods (I am pretty sure you do this with as_[format] but that’s beside the point) to include a new attribute… name_2 – this seems equally dumb
  3. Implement some kind of proxy class that is responsible for exposing only the methods that we’re after
  4. Let views handle creating some kind of hash that the versioned controllers and call to[format] on…

Three and Four are the only ones that I can actually think makes any kind of sense… Three would look something like:

# model
class Api::V1::Product < Struct.new(:product)
  def to_json
    attributes.to_json
  end

  def to_xml
    attributes.to_xml
  end

private
  def attributes
    {:name => product.name} # add all the attributes you want to expose
  end
end

# Controller
class Api::V1::ProductsController < V1Controller
  respond_to :json, :xml

  def show
    respond_with @product = Api::V1::Product.new(Product.find(params[:id]))
  end
end

What have other people done in the past?

  • 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-05-23T22:34:34+00:00Added an answer on May 23, 2026 at 10:34 pm

    Instead of one app serving V1 and V2 and V… you deploy one app for each version. One app is going to answer api.domain.com/v1, then another app is going to answer api.domain.com/v2 and so on.

    That is how service oriented applications are best organised, each service should be isolated, an independent deployment.

    Serving all versions from a single app defeats the purpose of service oriented design, since each time you make a change in one service you will need to test and deploy for all.

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

Sidebar

Related Questions

I am currently in the process of making a new ASP.net MVC website, and
I'm currently in the process of setting up a continuous integration environment at work.
I am currently in the process of creating my own blog and I have
We're currently in the process of setting up a source control/build/and more-server for .NET
I am currently in the process of restructuring my local Subversion repository by adding
I'm currently in the process of choosing a project for a grad-level compiler course
I am currently in the process of searching for a rules engine that works
I am currently in the process of rewriting an application whereby teachers can plan
I'm currently in the process of replacing my homebrewn build script by an Ant
I'm currently in the process of becoming a maintainer of a small open source

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.