Hello,
I am new to RoR and am coding a small budgetting app with two tables
(see below). As you can see every account has_many transactions and
every transaction has 2 accounts. How can I best respresent this using
ActiveRecord::Associations ?
create table accounts (
id int not null auto_increment,
name varchar(100) not null default ‘’,
primary key(id)
);
create table transactions (
id int not null
auto_increment,
month varchar(3) not null,
amount int null,
from_id int not null,
to_id int not null,
constraint fk_from_accounts foreign key (from_id) references
accounts(id),
constraint fk_to_accounts foreign key (to_id) references
accounts(id),
primary key(id)
) engine=InnoDB;
I see 3 possible solutions but nothing elegant:
- create 2 relationship tables (from and to)
- use sql
- code belongs_to_2 association
I would very much like your advice on this.
Thanks !
Simon