Hi,

found a strange behaviour in boss_db_adapter_mongodb.erl . Relates to 
ChicagoBoss-0.8.7

*if we have*
-module(
rec,
[Id
,MemId
,FromId
,ToId]).

-belongs_to(mem).
-belongs_to_mem(from).
-belongs_to_mem(to).

*and then we try to*

20> M2 = boss_db:find("rec-537015e2b3769c1e2b000012").
{rec,"rec-537015e2b3769c1e2b000012",
         "mem-536fa579b3769c1e2b000001",
         "from-5362580bb3769c433400001d", %it was Mem:id() at 'new' call, 
expect 'mem-5362580bb3769c433400001d' here
         "to-536242e9b3769c4334000002"} %it was Mem:id() at 'new' call, 
expect 'mem-536242e9b3769c4334000002' here
21> M2:from_id().                                         
"from-5362580bb3769c433400001d"
23> M2:from().
undefined

*it is clear that belongs_to_MODEL_NAME doesn't work as it supposed to. 
Made changes in boss_db_adapter_mongodb.erl (attached)*

% Convert a tuple return by the MongoDB driver to a Boss record
% 
mongo_tuple_to_record(Type, Row) ->
    MongoDoc = tuple_to_proplist(Row),
    AttributeTypes = boss_record_lib:attribute_types(Type),
    BelongsToTypes = boss_record_lib:belongs_to_types(Type), %added by 
can2nac, 14-05-14
    Args = lists:map(fun
            (id) ->
                MongoValue = attr_value(id, MongoDoc),
                unpack_id(Type, MongoValue);
            (AttrName) ->
                MongoValue = attr_value(AttrName, MongoDoc),
                ValueType = proplists:get_value(AttrName, AttributeTypes),
                unpack_value(AttrName, MongoValue, ValueType, 
BelongsToTypes) %added by can2nac, 14-05-14
                %unpack_value(AttrName, MongoValue, ValueType) %original, 
removed by can2nac, 14-05-14
        end, boss_record_lib:attribute_names(Type)),
    apply(Type, new, Args).



unpack_value(_AttrName, [H|T], _ValueType, _) when is_integer(H) -> 
{integers, [H|T]};
unpack_value(AttrName, Value, ValueType, BelongsToTypes) ->
  case is_id_attr(AttrName) and (Value =/= "") of
    true ->
      
unpack_id(proplists:get_value(list_to_atom(id_type_from_foreign_key(AttrName)),BelongsToTypes),Value);
    false ->
      boss_record_lib:convert_value_to_type(Value, ValueType)
  end.

%original, removed by can2nac, 14-05-14
%unpack_value(_AttrName, [H|T], _ValueType) when is_integer(H) ->
%    {integers, [H|T]};
%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.


*after changes and /ChicagoBoss-0.8.7$ ./rebar compile*

15> M1 = boss_db:find("rec-537015e2b3769c1e2b000012").
{rec,"rec-537015e2b3769c1e2b000012",
         "mem-536fa579b3769c1e2b000001",
         "mem-5362580bb3769c433400001d",
         "mem-536242e9b3769c4334000002"}
16> M1:from_id().
"mem-5362580bb3769c433400001d"
18> M1:from().   
{mem,"mem-5362580bb3769c433400001d"}

*I have not made any heavy tests. Just want to post it here or i'll forget 
about this))*

-- 
You received this message because you are subscribed to the Google Groups 
"ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/chicagoboss/e07ec28a-1eb6-4330-b22f-dde91f475df5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: boss_db_adapter_mongodb.erl
Description: Binary data

Reply via email to