I am getting ready to set up a resource for some new api calls to my rails application.
I am planning on calling the resource devices ie
resources :devices
This is going to represent android mobile devices
I know this will get me routes such as
GET devices/:id
In most cases :id would be an integer representing the primary key, and in the controller we would use :id as such:
GET devices/1
@device = Device.find(params[:id])
In this case I would like to use :id as the google_cloud_messaging_reg_id
So I would like to have requests like this:
GET devices/some_long_gcm_id
and then in the controller , just us params[:id]
to look up the device by the gcm registration id. This seem more natural, since the device will know it's gcm id rather than it's rails integer id. Are there any reasons I should avoid doing this?
---------------------------------------------------------------------
UPDATE:
My initial concern was that seeing a route (via rake routes) of /devices/:id or looking at the controller and seeing params[:id] would be confusing since most would assume :id was the integer primary key.
After thinking about it some more, and reading Idan Arye's answer below, my current thinking is to create routes like shown below. This will get me routes (via rake routes) that look like this:
GET /api/v1/android_devices/:gcm_reg_key(.:format)
which I think makes it somewhat clear that :gcm_reg_key may not be a primary key integer. And in the controller the parameter would be
params[:gcm_reg_key]
Again, I think this makes it somewhat clear that the param may not be an integer primary key (convention would be params[:id])
Any other thoughts or suggestions are welcome.
match '/android_devices/:gcm_reg_key', to: 'android_devices#show' , via: :get, as: 'android_device'
match '/android_devices/:gcm_reg_key', to: 'android_devices#update', via: :post, as: 'android_device_create'
match '/android_devices/:gcm_reg_key', to: 'android_devices#update', via: :patch, as: 'android_device_modify'
match '/android_devices/:gcm_reg_key', to: 'android_devices#destroy', via: :delete, as: 'android_device_destroy'
resources :android_devices , only: [:create, :index]