Re: belongs_to with different field then ClassnameId

42 views
Skip to first unread message

mpeter

unread,
Jan 5, 2013, 1:33:14 PM1/5/13
to chica...@googlegroups.com
Obviously, it is not a tree structure, it is just that I am experimenting with CB to test the functions I need.

On Saturday, January 5, 2013 6:45:04 PM UTC+1, mpeter wrote:
Hello,

I am trying to make a tree structure with CB but I have a problem with belongs_to... Namely, the getter function does not work when the field name does not match model name of the 'other side'.

What I have is 2 models
Usr(.....)
has(greetings many {greeting etc},{foreign_key, owner_id}).

Greeting(..., OwnerId)
belongs_to_usr(owner).
(belongs_to(owner). makes no difference)

If I add a new greeting with a specific user id, it gets saved properly, but the value in the OwnerId column changes from usr-1 usr-2 etc to oner-1, owner-2 etcwhen saved.

Now if I get a user instance, the following works just fine, it gives the greetings that belong to the user
Userinstance:greetings().

But it does not work in the reverse direction:
Greeting:owner(). returns undefined..

If I change OwnerId to UsrId, it works..

probably it has something to to with the id change upon saving (usr-1 > owner-1)

I think I am using a quite recent version of CB and MongoDB. Can anyone help me overcome this problem? Thanks a lot in advance!

Best,
Peter

Evan Miller

unread,
Jan 17, 2013, 12:45:58 PM1/17/13
to ChicagoBoss
Can anyone help him out?


--
 
 



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

Samuel Rose

unread,
Jan 17, 2013, 2:08:08 PM1/17/13
to chica...@googlegroups.com
On Sat, Jan 5, 2013 at 12:45 PM, mpeter <p.mo...@thecloudcasting.com> wrote:

> If I add a new greeting with a specific user id, it gets saved properly, but
> the value in the OwnerId column changes from usr-1 usr-2 etc to oner-1,
> owner-2 etcwhen saved.
>

The belongs_to(usr) in your model is superseding the belongs_to(owner)
somehow. Either that, or the variable where you are passing in the
OwnerId is being assigned to UsrId instead of OwnerId




--
--
Sam Rose
Hollymead Capital Partners, LLC
Cel: +1-(517)-974-6451
email: samue...@gmail.com
http://hollymeadcapital.com
http://p2pfoundation.net
http://socialmediaclassroom.com

"The universe is not required to be in perfect harmony with human
ambition." - Carl Sagan

mpeter

unread,
Jan 3, 2015, 5:02:22 PM1/3/15
to chica...@googlegroups.com
Dear All,

Sorry for not getting back earlier - notification did not came that it has been answered.
The problem is still present and very easy to reproduce.
Assume the following simple use case.
We have foo and bar. bar has an owner and a manager.

-module(foo,[Id,Name]).
-----
-module(bar,[Id, OwnerId, ManagerId]).
-belongs_to_foo(owner).
-belongs_to_foo(owner).
------------
simple test code with Mongo:
í{ok,{_,Id,_}}=(foo:new(id,"name")):save().
{ok,{_,Id2,_,_}}=(bar:new(id,Id,Id)):save().
B=boss_db:find(Id2).
B:owner().
B:manager().

the boss_db:find will rturn the object with changed (useless) ids. This is a serious problem which makes it impossible to have multiple belongs_to associations to the same type.

Is there a solution to overcome this?


Thanks,
Best,
Peter

mpeter

unread,
Jan 3, 2015, 5:20:25 PM1/3/15
to chica...@googlegroups.com
It is obviously the unpack_id(_,_) fun that messes up things in boss_db_adapter_mongodb.erl. It should respect the model defined by belongs_to_<model> when unpacking the id.

mpeter

unread,
Jan 3, 2015, 6:13:40 PM1/3/15
to chica...@googlegroups.com
A quick and dirty fix... There must be a better way to get the belongs_to types proplist though...

 diff boss_db_adapter_mongodb_old.erl boss_db_adapter_mongodb.erl
492c492
<                     unpack_value(AttrName, MongoValue, ValueType)
---
>                     unpack_value(Type, AttrName, MongoValue, ValueType)
547c547
< unpack_value(_AttrName, [H|T], _ValueType) when is_integer(H) ->
---
> unpack_value(_, _AttrName, [H|T], _ValueType) when is_integer(H) ->
549c549
< unpack_value(_AttrName, {_, _, _} = Value, datetime) ->
---
> unpack_value(_, _AttrName, {_, _, _} = Value, datetime) ->
551,565c551,568
< unpack_value(AttrName, Value, ValueType) ->
<     case is_id_attr(AttrName) and (Value =/= "") of
<         true ->
<             IdType = id_type_from_foreign_key(AttrName),
<             unpack_id(IdType, Value);
<         false ->
<             boss_record_lib:convert_value_to_type(Value, ValueType)
<     end.
<
< id_type_from_foreign_key(ForeignKey) ->
<     Tokens = string:tokens(atom_to_list(ForeignKey), "_"),
<     NameTokens = lists:filter(fun(Token) -> Token =/= "id" end,
<         Tokens),
<     string:join(NameTokens, "_").
<
---
> unpack_value(RecordType, AttrName, Value, ValueType) ->
>     case is_id_attr(AttrName) and (Value =/= "") of
>       true ->
>               BelongsToTypes=RecordType:belongs_to_types(boss_record:new(RecordType,[])),
>               AttrNameAtom=list_to_atom(string:join(lists:reverse(tl(lists:reverse(string:tokens(atom_to_list(AttrName), "_")))),"_")), % drop _id from the end
>               case proplists:get_value(AttrNameAtom,BelongsToTypes) of
>                       undefined ->
>                               unpack_id(atom_to_list(AttrNameAtom),Value)
>                               ;
>                       RealAttrType->
>                               unpack_id(atom_to_list(RealAttrType),Value)
>
>               end
>               ;
>       false ->
>               boss_record_lib:convert_value_to_type(Value, ValueType)
>     end
> .
Reply all
Reply to author
Forward
0 new messages