martenframework/marten

Make it possible to initialize new Model instances from query sets

Closed this issue · 0 comments

Description

It is presently not possible to initialize new model instances from a given query set. To palliate this, let's introduce a new #build query set method that will allow to initialize a new model instance from the specified attributes.

For example:

Author.all.build(name: "John Doe")

Initializing new records through the #build method should result in the initialized model instance being scoped to the record from which originated the query set in case of reverse query sets (ie. query sets that are created when reverse relations are defined with the related option of many_to_one_field).

For example:

author = Author.get!(id: 42)
book = author.books.build(title: "Super book")
book.author #=> author