2015-06-03 56 views
0

我有一個api的響應,我需要保存在我的模型中使用mongoid。如何在mongoid模型中保存數組的數組?

我相信響應是一個數組的數組,我似乎不能將它保存爲數組或散列。這是一個響應的例子。有誰知道我可以用mongoid保存這個文件嗎?

響應...

=> #<Tipper::User:0xb600c10 @accounts=[#<Tipper::Account:0xb5b8618 @id="QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", @name=nil, @type="depository", @meta={"number"=>"9606", "name"=>"Tipper Savings"}, @institution_type="fake_institution", @available_balance=1203.42, @current_balance=1274.93, @subtype=nil, etc... 

什麼辦法可以挽救這使我的應用程序可以參考呢?

我試圖使它使用這種方法的陣列...

def transactions 
    response = Tipper.set_user(access_token, ['auth']) 
    self.transactions = response.as_json 
    save 
    end 

它仍然不會工作

在JSON的反應是這樣的......

=> {"accounts"=>[{"id"=>"QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", "name"=>nil, "type"=>"depository", "meta"=>{"number"=>"9606", "name"=>"Tipper Savings"}, "institution_type"=>"fake_institution", "available_balance"=>1203.42, "current_balance"=>1274.93, "subtype"=>nil, "numbers"=>"Upgrade user to access routing information for this account"}, {"id"=>"nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", "name"=>nil, "type"=>"depository", "meta"=>{"number"=>"1702", "name"=>"Tipper Checking"}, "institution_type"=>"fake_institution", "available_balance"=>1081.78, "current_balance"=>1253.32, "subtype"=>nil, "numbers"=>"Upgrade user to access routing information for this account"}, {"id"=>"XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", "name"=>nil, "type"=>"depository", "meta"=>{"number"=>"5204", "name"=>"Tipper Premier Checking"}, "institution_type"=>"fake_institution", "available_balance"=>7205.23, "current_balance"=>7255.23, "subtype"=>nil, "numbers"=>"Upgrade user to access routing information for this account"}, {"id"=>"pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", "name"=>nil, "type"=>"credit", "meta"=>{"limit"=>12500, "number"=>"3002", "name"=>"Tipper Credit Card"}, "institution_type"=>"fake_institution", "available_balance"=>9930, "current_balance"=>2275.58, "subtype"=>nil, "numbers"=>"Upgrade user to access routing information for this account"}], "transactions"=>[{"id"=>"0AZ0De04KqsreDgVwM1RSRYjyd8yXxSDQ8Zxn", "account"=>"XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", "date"=>"2014-07-21", "amount"=>200, "name"=>"ATM Withdrawal", "location"=>{"city"=>"San Francisco", "state"=>"CA"}, "pending"=>false, "score"=>{"location"=>{"city"=>1, "state"=>1}, "name"=>1}, "cat"=>#<Tipper::Category:0xb6b2884 @type={"primary"=>"special"}, @hierarchy=["Transfer", "Withdrawal", "ATM"], @id="21012002">, "type"=>{"primary"=>"special"}, "category"=>["Transfer", "Withdrawal", "ATM"], "category_id"=>"21012002", "meta"=>{"location"=>{"city"=>"San Francisco", "state"=>"CA"}}}, {"id"=>"3mg4qV4JZycjewvKEzrLTYMzdr1MmvcO4Z3zX", "account"=>"XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", "date"=>"2014-07-24", "amount"=>240, "name"=>"Online Transfer from External Sav ...3092", "location"=>{}, "pending"=>false, "score"=>{"location"=>{}, "name"=>1}, "cat"=>#<Tipper::Category:0xb6b2604 @type={"primary"=>"special"}, @hierarchy=["Transfer", "Account Transfer"], @id="21001000">, "type"=>{"primary"=>"special"}, "category"=>["Transfer", "Account Transfer"], "category_id"=>"21001000", "meta"=>{"location"=>{}}}, {"id"=>"KdDjmojBERUKx3JkDdO5IaRJdZeZKNuK4bnKJ1", "account"=>"pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", "date"=>"2014-06-23", "amount"=>2307.15, "name"=>"Apple Store", "location"=>{"address"=>"1 Stockton St", "city"=>"San Francisco", "state"=>"CA"}, "pending"=>false, "score"=>{"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, "cat"=>#<Tipper::Category:0xb6b235c @type={"primary"=>"place"}, @hierarchy=["Shops", "Computers and Electronics"], @id="19013000">, "type"=>{"primary"=>"place"}, "category"=>["Shops", "Computers and Electronics"], "category_id"=>"19013000", "meta"=>{"location"=>{"address"=>"1 Stockton St", "city"=>"San Francisco", "state"=>"CA"}}}, {"id"=>"DAE3Yo3wXgskjXV1JqBDIrDBVvjMLDCQ4rMQdR", "account"=>"nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", "date"=>"2014-06-21", "amount"=>3.19, "name"=>"Gregorys Coffee", "location"=>{"address"=>"874 Avenue of the Americas", "city"=>"New York", "state"=>"NY"}, "pending"=>false, "score"=>{"location"=>{"address"=>1, "city"=>1, "state"=>1}, "name"=>0.2}, "cat"=>#<Tipper::Category:0xb6b2078 @type={"primary"=>"place"}, @hierarchy=["Food and Drink", "Restaurants", "Coffee Shop"], @id="13005043">, "type"=>{"primary"=>"place"}, "category"=>["Food and Drink", "Restaurants", "Coffee Shop"], "category_id"=>"13005043", "meta"=>{"location"=>{"address"=>"874 Avenue of the Americas", "city"=>"New York", "state"=>"NY"}}}, {"id"=>"1vAj1Eja5BIn4R7V6Mp1hBPQgkryZRHryZ0rDY", "account"=>"nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", "date"=>"2014-06-08", "amount"=>80, "name"=>"ATM Withdrawal", "location"=>{"city"=>"San Francisco", "state"=>"CA"}, "pending"=>false, "score"=>{"location"=>{"city"=>1, "state"=>1}, "name"=>1}, "cat"=>#<Tipper::Category:0xb6b1dd0 @type={"primary"=>"special"}, @hierarchy=["Transfer", "Withdrawal", "ATM"], @id="21012002">, "type"=>{"primary"=>"special"}, "category"=>["Transfer", "Withdrawal", "ATM"], "category_id"=>"21012002", "meta"=>{"location"=>{"city"=>"San Francisco", "state"=>"CA"}}}, {"id"=>"zq7MLAM4N3cjeKvXP9YqtBJXvZeajJCkjQakYv", "account"=>"QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", "date"=>"2014-06-02", "amount"=>-240, "name"=>"Online Transfer from Chk ...1702", "location"=>{}, "pending"=>false, "score"=>{"location"=>{}, "name"=>1}, "cat"=>#<Tipper::Category:0xb6b1ac4 @type={"primary"=>"special"}, @hierarchy=["Transfer", "Account Transfer"], @id="21001000">, "type"=>{"primary"=>"special"}, "category"=>["Transfer", "Account Transfer"], "category_id"=>"21001000", "meta"=>{"location"=>{}}}, {"id"=>"96d5AO5gLjC9EowVyn5OCBRjJR9LaOHJnBVJzd", "account"=>"nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", "date"=>"2014-06-01", "amount"=>240, "name"=>"Online Transfer to Sav ...9606", 

我模型字段看起來像這樣

Transaction.rb 
    class Transaction 
    include Mongoid::Document 

field :transactions, type: Array 

    def transactions 
    response = Tipper.set_user(access_token, ['auth']) 
    self.transactions = response.as_json 
    save 
    end 
end 

回答

1

你需要建立在一個蒙戈和Collection然後看看你的API響應,並決定要如何保存數據。它看起來像打印出某種類型的HTTP響應對象。你不能把它扔到數據庫中。如果HTTP響應包含JSON(例如),那麼您需要將其轉換爲數組。然後,將其指定爲您的Collection上一個新的Document數組屬性。

+0

好了,所以我將編輯我的問題談談你的答案 – SupremeA

+0

我不明白你的意思是設置的集合這種反應是這樣每次交易都能單獨項下交易的「收藏」保存交易記錄的列表。如果我理解你。我將如何設置一個集合?這是json – SupremeA