On Apr 18, 1:07 pm, Joel VanderWerf <[email protected]> wrote:
> Am I missing any special way of inserting model instances with cyclic
> references? Anything better than the following?
>
> require 'sequel'
>
> DB = Sequel.sqlite
>
> DB.create_table :foos do
>    primary_key :id
>    foreign_key :bar_id, :bars
> end
>
> DB.create_table :bars do
>    primary_key :id
>    foreign_key :baz_id, :bazs
> end
>
> DB.create_table :bazs do
>    primary_key :id
>    foreign_key :foo_id, :foos
> end
>
> class Foo < Sequel::Model
>    many_to_one :bar
>    one_to_many :baz
> end
>
> class Bar < Sequel::Model
>    many_to_one :baz
>    one_to_many :foo
> end
>
> class Baz < Sequel::Model
>    many_to_one :foo
>    one_to_many :bar
> end
>
> defer = []
>
> Foo.create do |foo|
>    foo.bar = Bar.create do |bar|
>      bar.baz = Baz.create do |baz|
>        defer << proc do
>          baz.foo = foo
>          baz.save
>        end
>      end
>    end
> end

I think this is a simpler way to do things:

  baz = Baz.create
  bar = Bar.create(:baz=>baz)
  foo = Foo.create(:bar=>bar)
  baz.update(:foo=>foo)

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