R.Bar
R.Bar

Reputation: 362

Versioning rails API

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

Upvotes: 0

Views: 1830

Answers (1)

MZaragoza
MZaragoza

Reputation: 10111

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

Upvotes: 2

Related Questions