ruby on rails - How do you save an array of arrays in a mongoid model? -
i have response api need save in model using mongoid.
i believe response array of arrays , cant seem save array or hash. here sample of response. know how can save 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 can save app can reference it?
i tried make array using 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 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 access routing information 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 access routing information 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 access routing information 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 access routing information 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 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 , electronics"], @id="19013000">, "type"=>{"primary"=>"place"}, "category"=>["shops", "computers , 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 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 , drink", "restaurants", "coffee shop"], @id="13005043">, "type"=>{"primary"=>"place"}, "category"=>["food , drink", "restaurants", "coffee shop"], "category_id"=>"13005043", "meta"=>{"location"=>{"address"=>"874 avenue of 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 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 sav ...9606", my model field looks
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
you'll need setup collection in mongo , take @ api response , decide how want save data. looks you're printing out type of http response object. can't throw in database. if http response contains json (for example) need convert array. then, assign array attribute on new document in collection.
Comments
Post a Comment