Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Saving relationships
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Graeme Defty  
View profile  
 More options May 15 2011, 11:22 pm
From: Graeme Defty <graeme.de...@gmail.com>
Date: Mon, 16 May 2011 10:22:41 +0700
Local: Sun, May 15 2011 11:22 pm
Subject: Re: Saving relationships

Evan,

Looks good to me.

g
________________________________
On 16 May 2011 02:28, Evan Miller <emmil...@gmail.com> wrote:

> Hi Andre,

> On Thu, May 12, 2011 at 1:52 PM, Andre Nathan <an...@digirati.com.br>
> wrote:
> > Hello

> > Say I have a model with multiple has_many relationships. For example,

> > -module(bookstore, [Id, Name]).
> > -has({employees, many}).
> > -has({books, many}).

> > Now say that when I create a new bookstore, the employes and books are
> > inputs in the bookstore form, so that in my controller I actually have
> > to save instances of the 3 models. I also want to abort the whole
> > process if any of those fail validation.

> > I'm doing something like this:

> > - Create a new bookstore
> > - Create the employees, with bookstore_id undefined
> > - Create the books, with bookstore_id undefined
> > - Call validate() on all those instances; if any of them fails, abort
> > - Save the bookstore
> > - Assign the bookstore ID to each employee and book
> > - Save the employees and books

> > The code for this can become somewhat complicated as more relationships
> > are added (not to mention really boring to write), and it seems wasteful
> > too because I have to call validate() then save(), which calls
> > validate() itself.

> > So, is there a better way do this? It would be cool if there was a way
> > to set related objects directly, something like

> >  B1 = bookstore:new(id, "foo"),
> >  B2 = B1:employees(ListOfEmployees),
> >  B3 = B2:books(ListOfBooks),
> >  B3:save()  % validates and saves each associated object too

> > Is it possible to add something like this to the API?

> The trouble with this approach is that the Bookstore object is really
> just a value-tuple and never includes any information about associated
> books or employees; these are fetched from the database each time
> employees() or books() is called. There's not a clean way that comes
> to mind for keeping track of associated non-saved objects.

> However, your problem might be better solved with database
> transactions.... maybe the API would look like

> ok = boss_db:transaction(fun() ->
>    Bookstore = bookstore:new(id, "foo"),
>    {ok, SavedBookstore} = Bookstore:save(),

>    lists:map(fun(Employee) ->
>         E1 = Employee:bookstore_id(SavedBookstore:id()),
>         {ok, E2} = E1:save()
>    end, ListOfEmployees),

>    lists:map(fun(Book) ->
>          ...
>    end, ListOfBooks)
> end)

> Any run-time error would result in the transaction being rolled back.
> What do you think?

> Not all databases support transactions but we could probably use the
> transaction semantics in Mnesia, MySQL, and Postgres at the very
> least.

> > Thanks,
> > Andre

> --
> Evan Miller
> http://www.evanmiller.org/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.