Sean Magyar
Sean Magyar

Reputation: 2380

RESTFUL endpoint for removing foreign key

I have a Rails API. What is the common practice (route and controller action) to remove foreign key in a REST API? I mean not deleting any resources just setting the foreign key to null on a one-to-one relationship.

For instance I have a vehicle and the vehicle has one customer.

If I was to delete the customer it would be:

DELETE /vehicles/:id/customer

If I just wanna set the vehicle_id: null on the customer what endpoint and controller I should use?

Upvotes: 0

Views: 456

Answers (1)

jvillian
jvillian

Reputation: 20263

I don't know what the common practice is, but you could set up your routes.rb something like:

Rails.application.routes.draw do

  ...

  resources :customers do 
    resources :vehicles, shallow: true
    member do
      put :remove_vehicle
    end
  end

  ...

end

Which will give you something like:

      customer_vehicles GET    /customers/:customer_id/vehicles(.:format)           vehicles#index
                        POST   /customers/:customer_id/vehicles(.:format)           vehicles#create
   new_customer_vehicle GET    /customers/:customer_id/vehicles/new(.:format)       vehicles#new
           edit_vehicle GET    /vehicles/:id/edit(.:format)                         vehicles#edit
                vehicle GET    /vehicles/:id(.:format)                              vehicles#show
                        PATCH  /vehicles/:id(.:format)                              vehicles#update
                        PUT    /vehicles/:id(.:format)                              vehicles#update
                        DELETE /vehicles/:id(.:format)                              vehicles#destroy
remove_vehicle_customer PUT    /customers/:id/remove_vehicle(.:format)              customers#remove_vehicle
              customers GET    /customers(.:format)                                 customers#index
                        POST   /customers(.:format)                                 customers#create
           new_customer GET    /customers/new(.:format)                             customers#new
          edit_customer GET    /customers/:id/edit(.:format)                        customers#edit
               customer GET    /customers/:id(.:format)                             customers#show
                        PATCH  /customers/:id(.:format)                             customers#update
                        PUT    /customers/:id(.:format)                             customers#update
                        DELETE /customers/:id(.:format)                             customers#destroy

In which case you will add a remove_vehicle action to your CustomersController. In that action, you will have access to params[:id] which you can use to find your @customer and then do something like:

class CustomersController < ApplicationController

  ...

  def remove_vehicle
    @customer = Customer.find(params[:id])
    @customer.update(vehicle_id: nil)
    redirect_to :somewhere
  end

  ...

end

Upvotes: 1

Related Questions