Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


CollectionProxy#find broken when UUID primary keys are used with has_many / inverse_of association #10450

bwbuchanan opened this Issue · 4 comments

3 participants


Given Post and Comment models having UUID primary keys, where Post has_many Comments:

post = Post.create!
comment = post.comments.create!

This should successfully find the newly-created comment. Instead, it throws ActiveRecord::RecordNotFound:

ActiveRecord::RecordNotFound: Couldn't find Comment with id=["bca2decc-51e4-4e58-b45b-6afae21c2168"] [WHERE "comments"."post_id" = $1]

This works fine, however:


As does this:


And this:


Rails version: 4.0.0.rc1

Minimal Rails app for reproducing the issue in the console:

@neerajdotname neerajdotname reopened this

This seems to only happen when the has_many relationship is specified with the "inverse_of" option, i.e.

class Post < ActiveRecord::Base
  has_many :comments, inverse_of: :post

Removing inverse_of: :post makes it stop happening.


OK, so the problem appears to be in ActiveRecord::Associations::CollectionAssociation#find_by_scan, which assumes that all object IDs are ints (and calls .to_i on the ids argument)

Not being an ActiveRecord associations expert, I'm not sure what the proper mechanism is for figuring out the actual data type of the target model's primary key, so that the type conversion can be adjusted accordingly.

Also, this function appears to load in the entire collection, just to find one member of it. That seems like it would make has_many :inverse_of associations perform terribly poorly once there are more than a handful of associated records. Wasn't :inverse_of supposed to optimize model loading?


@bwbuchanan I've sent PR #10500. Please review it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.