Nicholas Bayley


6,110 Experience
54 Lessons Completed
1 Question Solved


You are correct.

Your Bet model would have two belongs_to associations (one for the creator and one for the backer or however you'd like to call them):

class Bet < ApplicationRecord
  belongs_to :creator, class_name: 'User'
  belongs_to :backer, class_name: 'User'

Your bets table would need two columns for the user's id. With the above snippet, you would need a creator_id and a backer_id column.

And then if you wanted to access the bets from a user (created and backed bets for example) you would create two has_many associations on the User model:

class User < ApplicationRecord
  has_many :created_bets, foreign_key: 'creator_id', class_name: 'Bet'
  has_many :backed_bets, foreign_key: 'backer_id', class_name: 'Bet'

Hope that makes sense.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.