我有一个api的回复,我需要保存在使用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
答案 0 :(得分:1)
您需要在mongo中设置Collection
,然后查看您的API响应并确定您希望如何保存数据。您似乎正在打印某种类型的HTTP响应对象。你不能把它扔进数据库。如果HTTP响应包含JSON(例如),则需要将其转换为数组。然后,将其指定为Document
中新Collection
的数组属性。