Question about model associations

52 views
Skip to first unread message

Neil Pahl

unread,
May 9, 2014, 1:04:32 AM5/9/14
to chica...@googlegroups.com
I'm thinking about a "Stack Overflow" like scenario. Say I have the following models:

question
answer
comment
vote
appuser (user is reserved)

I would like questions and answers to have comments, and all three of those to have votes.

so, should my model for a comment look something like this?:

-module(comment, [Id, CommentText, AnswerId, QuestionId, AppuserId]).
-belongs_to(answer).
-belongs_to(question).
-belongs_to(appuser).
-compile(export_all).


However, I only want the Comment to belong to one of answer OR question. Can I leave AnswerId or QuestionId field blank when creating a comment record? 

Or, do I have to have less abstraction and create two more question/answer specific models like:

-module(questioncomment, [Id, CommentText, QuestionId, AppuserId]).
-belongs_to(question).
-belongs_to(appuser).
-compile(export_all).



and 

-module(answercomment, [Id, CommentText, AnswerId, AppuserId]).
-belongs_to(answer).
-belongs_to(appuser).
-compile(export_all).


Similar case with votes, expect I want each of question, answer, and comment to be able to get a vote... 1 vote models which can used for all 3? or 3 vote models for each q/a/comment.

Thanks in advance,

can2nac

unread,
May 9, 2014, 9:17:57 PM5/9/14
to chica...@googlegroups.com
So, why don't u try? )) It seems to be 5 mins of code.

"Can I leave AnswerId or QuestionId field blank when creating a comment record? " - yes, but you will get "05:11:38.235 [error] Error parsing Boss record id: error:function_clause", but it is not critical.
You can stay with 1 vote model, just add VoteId to all models.

can2nac

unread,
May 9, 2014, 9:19:45 PM5/9/14
to chica...@googlegroups.com
my comment is relevant for mongodb, by the way.

Neil Pahl

unread,
May 28, 2014, 8:30:19 AM5/28/14
to chica...@googlegroups.com
thanks can2nac, I tried it and it seems to work okay while using mock database. I'm not sure if it was behind all the problems I was having when I tried to change to Mnesia. It didn't seem to be a problem with Mongodb for me either.

I cast the fields to string using '::string()' in the model definition for vote, and then the empty field I defined using "" it it wasn't the same type via the little function:

optional_belongs(DataId, Type) ->
   
case head(string:tokens(DataId, "-")) =:= Type of
       
true ->
           
DataId;
       
false ->
           
""  
   
end.

head([H|_]) -> H.



can2nac

unread,
May 28, 2014, 11:31:42 AM5/28/14
to chica...@googlegroups.com
i didn't get the idea of optional_belongs(DataId, Type) to tell the truth. Give some code and console output, this may help to understand your case.

you should check source code of appropriate DB controller in case something is wrong. You can always add some trace points (i.e. io:format("something ~p~n",[A])) , make all ChicagoBoss and then run you project to understand if a controller or probably you is the reason of unpredictable behaviour.

By the way, there is a native hd\1 function to get head of list.
Reply all
Reply to author
Forward
0 new messages