Thanks for the quick reply, and the tip about nested_attributes.

Your suggestions solves the issue, in combination with fixing another
problem I had. I didn't RTFM, and was defining my one_to_one
incorrectly:

"Differences Between many_to_one and one_to_one
If you want to setup a 1-1 relationship between two models, you have
to use many_to_one in one model, and one_to_one in the other model."

Thanks again!



On Apr 23, 2:07 am, Jeremy Evans <[email protected]> wrote:
> On Sunday, April 22, 2012 6:16:28 AM UTC-7, James wrote:
>
> > Hi,
>
> > I've started working with sequel, forme and Sinatra. Seems to be a
> > nice combination, and have been learning a lot from reading the
> > source, but am stuck on an issue. I'd like to create a new object,
> > assign it to a one_to_one field using the = method, and generate
> > (empty) form fields using subform in forme.
>
> > I'm using :null=>false constraints and validations, so an object with
> > empty fields is invalid, and the save fails. Even if I disable
> > validation on implicit save using :validate => false, I run into the
> > NOT NULL constraint at the database level, so how can I disable the
> > implicit save completely? Relevant code:
>
> > class Report
> >     one_to_one :action, :validate=>false
> > ...snip
>
> > # field with a constraint
> > class Action
> >     Date :agreed_completion_date, :null=>false
> > ...snip
>
> > # Run into the constraint here (implicit save in setter method)
> > if report.action.nil? then report.action = Action.new end
>
> > In associations.rb, and there don't seem to be any options to disable
> > the save. The comments suggest autosaving is not done for one_to_many,
> > so I could use that, but the actual auto-generated methods in
> > one_to_many seem to have saves in them. I'm sure there is a good
> > reason for this, but any suggestions on how I can make this work are
> > appreciated.
>
> The problem you are having is that Action.new is an invalid action.  You
> shouldn't be assigning it to report.action until it is a saveable state.
>
>   report.action = Action.new(:agreed_completion_date=>...) unless
> report.action
>
> An alternative that doesn't require saving is doing
>
>   action = Action.new(:report=>report) unless report.action
>
> And then saving the action later.  This requires that report is already
> saved in the database, though.
>
> In general, when using subforms for associated objects, you're going to
> want to use the nested_attributes plugin.  The forme Sequel plugin is
> specifically designed to be used with nested_attributes.
>
> Thanks,
> Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to