2015-06-15 57 views
-1

我想这样做像水木清华:Rails的哈希添加到现有的哈希

a = Hash.new 
a = {:profile => @user} 
a[:profile][:contacts] = Hash.new 
a[:profile][:contacts] = @user.contacts.all 

但我得到的错误不能写未知属性'接触。我们的数据库中有select的结果。 我需要创建一个散列这种结构

[:profile][:name] = boris 
[:profile][:sex] = 1 
[:propfile][:contacts] = here anotrher hash 

回答

1

解决的办法是转换后,从选择到散列结果分贝使用@ user.as_json

a = Hash.new 
    a[:profile] = @user.as_json 
    a[:profile][:contacts] = @user.contacts.all