how to build a json from two model

38 views
Skip to first unread message

Gustavo A. León Tramontin

unread,
Feb 7, 2014, 6:47:36 PM2/7/14
to chica...@googlegroups.com
Hi all, i'm starting to work with Erlang and CB, and i'm building a RESTful api, and i have a question about how to build the json to access data from one model to another

My model and my controller looks like this:


%% review model
-module(review,
    [
        Id,
        UsersId,
        Score::integer(),
        Speed::integer(),
        Comment::string(),
        Date::datetime()
    ]
).
-compile(export_all).
-belongs_to(users).



%% users model
-module(users,
    [
        Id,
        FirstName::string(),
        LastName::string(),
        Email::string()
    ]
).
-compile(export_all).
-has({reviews, many}).



%% my review controller:

api('GET', [Id]) ->
    Review = boss_db:find(Id),
    case Review of
        Review ->
            {json, [{review, Review}]};
        [] ->
            not_found
    end;


%% and my current json is as follows


       [{
          "id":"review-1",
          "users_id":"users-1",
          "score":1,
          "speed":1,
          "comment":"foo",
          "date":"February 07, 2014 21:52:53",
       }]



and my question is how to access the data from the "users-1"

Thank you very much, and sorry for the length of the message

Cuong Thai

unread,
Feb 7, 2014, 11:28:22 PM2/7/14
to chica...@googlegroups.com
Hi Gustavo,

With your implementation, you can access data of "users-1", for example FistName, by getting {{ review.users.first_name }} in template. (Refresh document for details http://localhost:8001/doc/)

Refer API record for more options, for example you can specify which column for foreign key.

Regards,
Cuong Th.
Reply all
Reply to author
Forward
0 new messages