0

I was trying to find a way to manage different versions for my rails API. Different routing is not a problem, the problem begins when i'm changing the models and the database (Postgres) between different versions. What is the best practice for managing different versions in rails API ? Thanks guys

Edit: Example -the problem arrives when i'm changing one of the model from V1 to V2. Lets say that at V1 i has a model called 'Product' that I accidently saved the 'price' property in string instead of integer, at V2 i saw the problem and made a migration that fixs the problem. The fix made a new problem,now V1 API is broken because is trying to take string from integer column

1
  • Are you trying to maintain some level of backwards compatibility? Commented Sep 16, 2016 at 15:01

1 Answer 1

2

What I like to do is create different endpoint

1) set your routes

namespace :api do

  namespace :v1 do
    resources :...
  end

  namespace :v2 do
    resources :...
  end

end

Now that you have your end points you can create your controllers The way I like to do it is

#app/controller/api_controller.rb
class ApiController < ActionController::Base

  layout false
  ...

end

Now you have a folder in your controller folder for each version

app/controllers/api/v1 app/controllers/api/v2

Now in each your expose what your need

class Api::V1::MooController < ApiController

end

update

You can not remove the string value of price but you have to create a new field call int_price. That way api v1 can still properly respond to the string price

Now you would also need a method that would populate the int_price when ever the string price gets updated

note

I don't think that your should have a new endpoint just to change a price from string to an integer you could just write a method if string convert to integer

I hope that this helps

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, first of all thank you. secondly i added to my question much more details about the problem that I'm facing. I would you like you read this please
Stripe seems to have a pretty interesting approach: stripe.com/blog/api-versioning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.