2015-06-15 67 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