SupremeA
SupremeA

Reputation: 1621

Getting model to save after running method

I have a model that creates and then runs several API calls to fill in the fields. I can get the API calls to run properly in my console but when I try and run it in the app it does not save and I am unsure it runs the method.

Here is the model...

transaction.rb

  field :transactions
  field :access_token
  field :public_token
  field :accounts

  def token_exchange
    exchangeTokenResponse = API.exchange_token(public_token)
    self.access_token = exchangeTokenResponse.access_token
    self.accounts = API.set_user(access_token, ['auth'])
    self.transactions = API.set_user(access_token, ['connect'])
  end
end

And my controller is here...

transactions_controller.rb

def exchange_token
    current_tenant.create_transaction(:public_token => params[:public_token])
    current_user.transaction.token_exchange
    redirect_to ...
  end

end

When the exchange_token route is called it should run the method in the model, save the responses to the respective fields in the model and then redirect to the next page. I cant get it to work because it doesnt seem to be saving or Im not sure its even running the method but when I put a raise item in it shows that it does go thru the method. Any idea what I am doing wrong?

My console shows this after it runs

Started GET "/users/transactions/exchange_token?public_token=..." for 10.0.2.2 at 2015-06-01 18:47:58 +0000
Processing by Users::TransactionsController#exchange_token as HTML
  Parameters: {"public_token"=>"..."}
D, [2015-06-01T18:47:59.477157 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.6326ms
D, [2015-06-01T18:47:59.478691 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=lease_defend_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('....')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.7417ms
D, [2015-06-01T18:47:59.495108 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 QUERY        database=development collection=transactions selector={"$query"=>{"user_id"=>BSON::ObjectId('...')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.4533ms
D, [2015-06-01T18:47:59.497133 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 DELETE       database=development collection=transactions selector={"_id"=>BSON::ObjectId('...')} flags=[:remove_first]
D, [2015-06-01T18:47:59.497519 #3521] DEBUG -- :                          COMMAND      database=development command={:getlasterror=>1, :w=>1} runtime: 0.4444ms
D, [2015-06-01T18:47:59.499636 #3521] DEBUG -- :   MOPED: 127.0.0.1:27017 INSERT       database=development collection=transactions documents=[{"_id"=>BSON::ObjectId('...'), "public_token"=>"...", "user_id"=>BSON::ObjectId('...')}] flags=[]
D, [2015-06-01T18:47:59.500036 #3521] DEBUG -- :                          COMMAND      database=development command={:getlasterror=>1, :w=>1} runtime: 0.3621ms
Redirected to http://0.0.0.0:3000/

EDIT- I am getting this response and I need to put it into a field to call on later. What is this a Hash? Array? or what...

#<Api::User:0xb7e9f2c @accounts=[#<Api::Account:0xb7b6eb0 @id="QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK", @name=nil, @type="depository", @meta={"number"=>"9606", "name"=>"Bank Savings"}, @institution_type="fake_institution", @available_balance=1203.42, @current_balance=1274.93, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900009606", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b6ba4 @id="nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy", @name=nil, @type="depository", @meta={"number"=>"1702", "name"=>"Bank Checking"}, @institution_type="fake_institution", @available_balance=1081.78, @current_balance=1253.32, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900001702", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b68e8 @id="XARE85EJqKsjxLp6XR8ocg8VakrkXpTXmRdOo", @name=nil, @type="depository", @meta={"number"=>"5204", "name"=>"Bank Premier Checking"}, @institution_type="fake_institution", @available_balance=7205.23, @current_balance=7255.23, @subtype=nil, @numbers={"routing"=>"021000021", "account"=>"9900005204", "wireRouting"=>"021000021"}>, #<Api::Account:0xb7b66cc @id="pJPM4LMBNQFrOwp0jqEyTwyxJQrQbgU6kq37k", @name=nil, @type="credit", @meta={"limit"=>12500, "number"=>"3002", "name"=>"Bank Credit Card"}, @institution_type="fake_institution", @available_balance=9930, @current_balance=2275.58, @subtype=nil, @numbers={}>], @transactions=[], @permissions=["auth"], @access_token="test_chase", @api_res="success", @info={}, @pending_mfa_questions="", @type=nil>

Upvotes: 1

Views: 35

Answers (1)

Rokibul Hasan
Rokibul Hasan

Reputation: 4146

The problem is, you did not call any save after calling the method token_exchange, rewrite your method as following, it will save your transaction

 def token_exchange
   exchangeTokenResponse = API.exchange_token(public_token)
   self.access_token = exchangeTokenResponse.access_token
   self.accounts = API.set_user(access_token, ['auth'])
   self.transactions = API.set_user(access_token, ['connect'])
   self.save
 end

Upvotes: 2

Related Questions