Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Validating number of associated objects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andrew Cove  
View profile  
 More options Aug 31 2012, 1:10 am
From: Andrew Cove <andrew.c...@gmail.com>
Date: Thu, 30 Aug 2012 22:10:17 -0700 (PDT)
Local: Fri, Aug 31 2012 1:10 am
Subject: Validating number of associated objects

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...
but 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.

Are InstanceHooks the solution? Is there a standard implementation of a
solution?

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)
-- Do I need to use a transaction?
-- 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?

Thanks,
Andrew


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Evans  
View profile  
 More options Aug 31 2012, 11:38 am
From: Jeremy Evans <jeremyeva...@gmail.com>
Date: Fri, 31 Aug 2012 08:38:24 -0700 (PDT)
Local: Fri, Aug 31 2012 11:38 am
Subject: Re: Validating number of associated objects

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Cove  
View profile  
 More options Aug 31 2012, 12:18 pm
From: Andrew Cove <andrew.c...@gmail.com>
Date: Fri, 31 Aug 2012 09:18:45 -0700 (PDT)
Local: Fri, Aug 31 2012 12:18 pm
Subject: Re: Validating number of associated objects

Thanks Jeremy.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »