Postgres UUID validation prevents ActiveRecord::RecordNotFound being thrown in find #11957

te-chris opened this Issue Aug 21, 2013 · 8 comments


None yet
4 participants

I've just switched a project over to use UUID primary keys and have encountered an interesting issue. Postgres (understandably) validates UUID input on SELECT requests and will throw an error if you pass in a malformed UUID. This is fine behaviour, but for AR::Base.find, where the standard pattern is to pass in whatever is in params[:id], this will result in users being shown 500 errors whenever a malformed UUID is passed in, instead of it just failing silently to 404.

I'm not sure whether the best behaviour is to catch the malformed UUID exception in find, or whether to just leave it, but allowing 500 errors to be thrown at will based on user input seems to be the wrong option. I'm posting this as an issue because it seems like a common edge case that should be dealt with, but I don't really know what the correct way to deal with in the Rails framework is, so hopefully more experienced heads can weigh in?


jiripospisil commented Aug 21, 2013

Related #11902


senny commented Aug 26, 2013

I'm closing this one in favor of #11902

@senny senny closed this Aug 26, 2013


senny commented Nov 14, 2013

@te-chris I'll reopen this as #11902 was about find_by, which has different semantics than find.

@senny senny reopened this Nov 14, 2013

Thanks @senny, do you have any preference as to how this should be handled? ie. catching the PG error or validating before we send to PG?

rafaelfranca added a commit that referenced this issue Aug 22, 2014

Merge pull request #15932 from joseluistorres/issue-11957-uuids
adding the exception RecordNotFound to UUID find #11957

Hey @rafaelfranca, I see that you merged a patch in for this. Has this been deployed in 4.1.5? I'm trying to follow but getting confused by the commit logs. Also, does this fix the behaviour of all the AR finder methods or just #find?


rafaelfranca commented Sep 17, 2014

Just 4.2


rafaelfranca commented Sep 17, 2014

And it will work for any finder

Awesome! great work and thanks!  Guess I should get onto the 4.2 beta.
Christopher Bull

Skype: bull-chris

On 18 September 2014 at 6:37:38 am, Rafael Mendonça França ( wrote:

And it will work for any finder

Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment