Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
bwbuchanan opened this Issue · 4 comments

3 participants

@bwbuchanan

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

post = Post.create!
comment = post.comments.create!
post.comments.find(comment.id)

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:

post.comments.first

As does this:

post.comments.where(id: comment.id).first!

And this:

Comment.find(comment.id)
@bwbuchanan

Rails version: 4.0.0.rc1

Minimal Rails app for reproducing the issue in the console:

https://dl.dropboxusercontent.com/u/2829943/rails-issue-10450.tgz

@neerajdotname neerajdotname reopened this
@bwbuchanan

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
end

Removing inverse_of: :post makes it stop happening.

@bwbuchanan

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?

@kennyj
Collaborator

@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.