mb14
mb14

Reputation: 22616

How do I save a model recursively with Sequel?

I've been playing with Sequel and Sequel::Model.

I created an Group with a many Items (one_to_many).

I can do:

Group.new << Item.new

but not:

Group.new.add_item(Item.new)

nor:

Item.new.group=Group.new. 

It complains about Group not having a primary key.

If I save group, it's saved but the items are not saved.

How can I do a recursive save of everything?

Upvotes: 3

Views: 561

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

Sequel by design does not save entire object graphs. Its association modification methods are designed to be very direct and not offer a lot of abstraction.

You probably want to use the nested_attributes plugin or the instance_hooks plugin (which the nested_attributes plugin uses internally).

# nested attributes plugin
Group.new(:items_attributes=>[{}]).save

or

# instance_hooks plugin
Group.new.after_save_hook{add_item(Item.new)}.save

Upvotes: 5

Related Questions