Reputation: 1621
I have a response from an api that I need to save in my model which is using mongoid.
I believe the response is an array of arrays and I cant seem to save it as an array or hash. Here is a sample of the response. Does anyone know how I can get this to save with mongoid?
Response...
=> #<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...
Any way I can save this so my app can reference it?
I tried to make it an Array using this method...
def transactions
response = Tipper.set_user(access_token, ['auth'])
self.transactions = response.as_json
save
end
It still wont work
The response in json looks like this...
=> {"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",
My model field looks like this
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
Upvotes: 0
Views: 259
Reputation: 11409
You'll need to setup a Collection
in mongo and then take a look at your API response and decide how you want to save the data. It looks like you're printing out some type of HTTP response object. You can't just throw that in the database. If the HTTP response contains JSON (for example) then you need to convert that to an array. Then, assign it as an array attribute on a new Document
in your Collection
.
Upvotes: 1