'How to assign an instance to a variable - .find() / .where() - and access it's values - RUBY on RAILS

account = Account.find(params[:account_id])

account_to_receive = Account.where(number_account: transaction.account_to_transfer)

So I am building a bank app I would like to transfer an amount to another bank account but accessing it through it's number_account

Assigning with where() what I get #Account::ActiveRecord_Relation:0x4290 So can not access it's values

Differently from using .find() So I get #<Account:0x00007f39a45e5760 And I could then access everything

Anyone to bright me on how to do that?



Solution 1:[1]

account = Account.find(params[:account_id])

account_to_receive = Account.find_by(number_account: transaction.account_to_transfer)

# Credit `account_to_receive` from the amount from `account`

But pay attention, this means you already know the 2 accounts. If you're using IBAN, it seems difficult to have all accounts stored in your database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brcebn