You can clone with
HTTPS or Subversion.
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:
Rails version: 4.0.0.rc1
Minimal Rails app for reproducing the issue in the console:
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 :)