Reputation: 916
I'm new to rails, and I would like to get your help in this issue.
I have two tables Account and Transaction. each transaction shall have from account and to account. so when i add a transaction i shall select from the account table two times to get the FROM and TO accounts (list Selection in new transaction Form).
I'm using scaffold to generate the code and I can add only one relation to the table. Do you how to generate the relation with the account table two times
This is the generation code:
$rails g scaffold account a_name:string a_type:string a_amount:float
$rails g scaffold Transaction account_id:integer account_id:integer t_amount:float t_date:date t_desc:text
Upvotes: 1
Views: 282
Reputation: 35349
You will need two account_id
columns in the transactions table, from_account_id
and to_account_id
, otherwise how will you tell the difference between the two?
rails g scaffold Transaction to_account_id:integer from_account_id:integer
Then, in your Transaction model, you will need to define the relationship properly:
belongs_to :from_account, class_name: 'Account'
belongs_to :to_account, class_name: 'Account'
As a side note, you really should avoid using scaffolding since it makes too many decisions and generalisations for you. You can use individual generators.
rails g model Transaction from_account_id:integer to_account_id:integer
rails g controller transactions
Upvotes: 2