Search code examples
ruby-on-railsdatabaseruby-on-rails-3routesfindby

howto find_by ownkey the exact entry in rails


we have an show-action which should find by the own_key the right entry. The own_key is generated as UUIDTools::UUID.timestamp_create().to_s. The following question is now here.

class ListController < ApplicationController
  respond_to :html, :xml, :json
  def show
    @list = List.find_by_own_key(params[:own_key])
    respond_with(@list)
  end
end

the routes are here so generates

resources :lists
match '/:id' => 'list#show'

why did we get also an entry back if we only type one simple letter after the /? The own_key look so f6d47c20-a276-11e1-b127-68a3c454c2b4. So if we type an /lists/f i get the entry with an f own_key. how can we manage that we only get the entry with the own_key? Could it run by an contraint?

thanks for the help if anone can help us?

Marcus


Solution

  • From your routes, params[:id] will contain the I'd to search for, however you're using params[:own_key] which will be nil. Instead of searching for the record with the specified value of own_key your code will always fetch the row with a null own_key.

    Change your code to use params[:id] and you should be ok