On Friday, August 31, 2012 11:38:24 AM UTC-4, Jeremy Evans wrote:
> On Thursday, August 30, 2012 10:10:17 PM UTC-7, Andrew Cove wrote:
>> I'm trying to figure out how to handle this situation in Sequel. I'm
>> trying to create Meetings, where each Meeting occurs at a Venue, and
>> Meetings are attended by Attendees. In order to create a meeting, there
>> must be a positive number of attendees.
>> Meetings have a foreign key for their venue, but use a join table to
>> handle attendees. An attendee might have multiple meetings in the same
>> venue at different times (so there could be more than one join table row
>> with the meeting id and attendee id).
>> I'm coming from the AR world, and I'm trying to understand how to do the
>> following in Sequel: I want to only create the meeting record and the rows
>> in the join table if there are attendees.
>> This topic has been discussed on the forums before: (the most extensive
>> one is here
>> https://groups.google.com/forum/?fromgroups=#!searchin/sequel-talk/de... that didn't seem to result in a plugin). The fundamental issue is that
>> the Meeting record doesn't have a primary key yet, so the join table
>> records can't be created, so there's no direct way to validate the number
>> of attendees before saving.
> Not much has changed in regards to this in terms of Sequel's default
> behavior.
>> Are InstanceHooks the solution? Is there a standard implementation of a
>> solution?
> 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
>> 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.
>> Questions:
>> -- Where/how should I store the deferred objects? (putting them in
>> self[:deferred_objects] caused problems upon saving)
> You don't want to use self[:deferred_objects] for that, as that assumes
> :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
>> deferred objects? or is there a single command to create all of the join
>> table rows?
> One call per object. If this turns into a performance bottleneck
> (unlikely), you could try using Dataset#import to insert multiple rows in a
> single query (on some databases).
> Thanks,
> Jeremy