On Feb 23, 8:45 pm, Sandeep <[email protected]> wrote:
> On Feb 23, 11:40 pm, Jeremy Evans <[email protected]> wrote:> In this
> particular case, I think this will work:
>
> > Main.many_to_one :derived, :key=>:id
> > Derived.many_to_one :main, :key=>:id
>
> > You could use one_to_one as well, but many_to_one is simpler and
> > should work.
>
> Hi Jeremy,
> Thanks for replying.
>
> What is the sequence of creates and saves that I should use to get
> this done. If I do something like this:
>
> m = Main.create(:other_field => 1)
> d = Derived.create(:other => 11)
> m.derived = d
> m.save
> this doesnt implicitly save d as weel. Probably because Sequel doesnt
> use proxies.
>
> Should I be doing this:
> m = Main.create(:other_field => 1)
> d = Derived.create(:other => 11)
> m.save
> m.add_derived(d) # at THIS point, does d get the primary key of
> m's :id ?
The simplest way to do what you want is:
Derived.create(:other=>11, :main=>Main.create(:other_field=>1))
You can also use the instance hooks plugin:
Main.new(:other_fields=>1).after_create_hook{Derived.create(:main=>self,
:other=>11)}.save
Or the nested_attributes plugin:
Main.create(:derived_attributes=>{:other=>11}, :other_fields=>1)
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.