[ActiveRecord] Proposal to support associations using array columns

Hey Everyone.

Last year I implemented array association on my GEM exclusively for PostgreSQL:

https://github.com/crashtech/torque-postgresql

https://github.com/crashtech/torque-postgresql/wiki/Has-Many

https://github.com/crashtech/torque-postgresql/wiki/Belongs-to-Many

I’m planning to push this feature to Active Record, so any database that supports array could use.

The main steps that I took was:

  1. Allow array setting for has_many

  2. Identify array associations with something like connected_through_array?

  3. Create a generic method that infers the correct way to connect both keys (primary_key and foreign_key), instead of direct use of table[key].eq(foreign_table[foreign_key]

  4. Allow cast to be called on Some Arel Nodes

  5. Make sure that AssociationScope rely on the above generic method to build the condition

  6. Create the correct BindParam for each value on an array association

  7. Create the necessary BelongsToManyReflection, BelongsToManyAssociation, and Builder::BelongsToMany

  8. Adjust AutosaveAssociation to behave properly against array associations

  9. A couple of other minors adjusts to make it seamless to other associations

The only problem so far is that it doesn’t work with Polymorphic, but, it’s so far a big win, especially for systems with tags and conversations (with multiple users).

I was thinking about pushing it to Rails not only because it’s a cool and handy feature, but also because one of the problems that I faced recently with the GEM, where the eq condition is hardcoded, and that annoys me since it could open a door for many other customized associations. Here are the commit and line that got me thinking about this: https://github.com/rails/rails/commit/54de9b1e4f5823bf22001be60efcb996ce6d260b#diff-1663e5f930ac83fef235d7e2aa5edf65R185

I want to hear people’s opinions. And, if I decided to move forward, I’ll do one step at a time, probably starting by the generic method on step #3.