rails/rails — pull request #16277 by razum2um (2014-07-24 08:47:58+00:00)
Title: AR relation accepts :primary_key as proc

Sometimes you need the power of eager loading with `includes` to avoid N+1 queries, but primary key for link is not always the same.

I suggest to give a possibility to specify arbitrary keys for loading with `includes`:
- You define a proc which accepts an instance returns a key for that will be searched in the database.
- This proc returns ID's (one or many) of linked rows in another table and they will be fetched if `includes` is used.

I think, this is more flexible in comparison with just a column name anyway.
Of course, joins becomes impossible for this case, and I raise in this case,
but you can always specify multiple relations (one for join, another for include) if needed.

P.S.
The example given in tests I provide may look obscure, but imagine another use case:
- you have a hierarchy of tags bound to to things
- you need to fetch tags (flat) based on criteria
- each tag should have a group of things that are linked either to this tag, or it's it parents (which id's are stored in ancestry).
I'd like to provide this in tests but tags haven't neither parent_id nor ancestry, and I used Comment instead