Not much has changed in regards to this in terms of Sequel's default
> Are InstanceHooks the solution? Is there a standard implementation of a Instance hooks can be used to implement saving of associated objects during
Model#save. That's how the nested_attributes plugin uses them internally.
The nested_attributes plugin does something similar to what you want
already. With the nested_attributes plugin, the to-be-saved objects are
stored in the associations hash, so you could add a validation like this:
errors.add(:association, "does not have enough objects") unless
association.length >= some_number
You don't want to use self[:deferred_objects] for that, as that assumes
> I'm tempted to try implement it myself, but am still working out the
> details. It seems to involve storing the pending associations in the model,
> and then creating the join rows after saving.
> -- Where/how should I store the deferred objects? (putting them in
> self[:deferred_objects] caused problems upon saving)
:deferred_objects is a model column. Since it's an association, using the
associations hash makes the most sense. You could use a plain instance
variable as well.
> -- Do I need to use a transaction? Model#save uses transactions by default, and that shouldn't be turned off
if you are issuing multiple queries inside save.
> -- In the after_create_hook, do I call add_association on all of the One call per object. If this turns into a performance bottleneck
> deferred objects? or is there a single command to create all of the join
> table rows?
(unlikely), you could try using Dataset#import to insert multiple rows in a
single query (on some databases).