martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
425 stars 24 forks source link

Make it possible to initialize new Model instances from query sets #236

Closed ellmetha closed 5 months ago

ellmetha commented 5 months ago

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