Skip to content

Commit

Permalink
Add has_many Debts relationship to Bet
Browse files Browse the repository at this point in the history
This will help easily diplay or manipulate the Debts that are associated
with a particular Bet.
  • Loading branch information
LeonTenko committed Mar 27, 2017
1 parent 9730661 commit d6db150
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/models/bet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Bet < ApplicationRecord
has_many :users, through: :user_bets
has_many :user_bets
has_many :options, class_name: 'BetOption'
has_many :debts
belongs_to :creator, class_name: 'User'

validates :expires_at, presence: true
Expand All @@ -15,7 +16,8 @@ def resolve(winning_option:)
Debt.create(
creditor: won_bet.user,
debtor: lost_bet.user,
amount: amount_owed(lost_bet, won_bet)
amount: amount_owed(lost_bet, won_bet),
bet: self
)
end
end
Expand Down
1 change: 1 addition & 0 deletions app/models/debt.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Debt < ApplicationRecord
belongs_to :debtor, class_name: 'User'
belongs_to :creditor, class_name: 'User'
belongs_to :bet, optional: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddHasManyBelongsToRelationshipBetweenBetAndDebt <
ActiveRecord::Migration[5.0]
def change
add_reference :debts, :bet, foreign_key: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170323160837) do
ActiveRecord::Schema.define(version: 20170323164658) do

create_table "bet_options", force: :cascade do |t|
t.text "option_text"
Expand All @@ -37,6 +37,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "payment_date"
t.integer "bet_id"
t.index ["bet_id"], name: "index_debts_on_bet_id"
t.index ["creditor_id"], name: "index_debts_on_creditor_id"
t.index ["debtor_id"], name: "index_debts_on_debtor_id"
end
Expand Down

0 comments on commit d6db150

Please sign in to comment.