私は借金を解決し、誰が誰に何を負っているのかなどを解決するアプリに取り組んでいます。
現在、次のように機能します。
create_table "debts", :force => true do |t|
t.string "amount"
t.integer "payer_id"
t.integer "payee_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "people", :force => true do |t|
t.string "name"
t.integer "bank"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
class Debt < ActiveRecord::Base
attr_accessible :amount, :payee_id, :payer_id
belongs_to :payee, :class_name => 'Person'
belongs_to :payer, :class_name => 'Person'
end
class Person < ActiveRecord::Base
attr_accessible :amount, :payee_id, :payer_id
has_many :debts_owed, :class_name => Debt, :foreign_key => "payee_id"
has_many :debts_to_pay, :class_name => Debt, :foreign_key => "payer_id"
end
それは機能していますが、同じモデルへの複数の関連付けを表すより簡単な方法が必要であることを私は知っていますか?私はhas_and_belongs_to_manyを読んでいますが、これは正しいことのように見えますが、正直に言うと迷っています。
どんな助けでもいただければ幸いです!
ありがとう