Saturday, 3 September 2022

INNER JOIN AND defaults routes with json

 Get states based country:

    @states = State.joins("INNER JOIN countries c on c.id = states.country_id").where("c.id LIKE ?", params[:country_id])


Get county based state name:

@countries = Country.joins("INNER JOIN states s on s.country_id = countries.id").where("LOWER(s.name) LIKE ?", "%params[:name].downcase%")

Set default JSON at routes:

resources :states, :defaults => { :format => 'json' }
resources :countries, :defaults => { :format => 'json' }
get '/get_states/:country_id' => 'states#get_states_based_county', :defaults => { :format => 'json' }


-------------------------------------------------------------------------------------------
resources routes

# resources :users, :defaults => { :format => 'json' }
      get '/users' => 'users#index', :defaults => { :format => 'json' }
      get '/user/:id' => 'users#show', :defaults => { :format => 'json' }
      post '/user' => 'users#create', :defaults => { :format => 'json' }
      put '/user/:id' => 'users#update', :defaults => { :format => 'json' }
      delete '/user/:id' => 'users#destroy', :defaults => { :format => 'json' }
      get '/typeahead/:input' => 'users#search', :defaults => { :format => 'json' }