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:
-
Allow
array
setting forhas_many
-
Identify array associations with something like
connected_through_array?
-
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]
-
Allow
cast
to be called on Some Arel Nodes -
Make sure that
AssociationScope
rely on the above generic method to build the condition -
Create the correct
BindParam
for each value on an array association -
Create the necessary
BelongsToManyReflection
,BelongsToManyAssociation
, andBuilder::BelongsToMany
-
Adjust
AutosaveAssociation
to behave properly against array associations -
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.