In Rails, how would one conditionally associated records on a
class Physician < ApplicationRecord
has_many :patients, through: :appointments
class Appointment < ApplicationRecord
class Patient < ApplicationRecord
has_many :physicians, through: :appointments
For the original question, a
connection doesnt make a difference between the two users, so i would model it as a
one to many relationship and validate it only has two users.
A like has two users, the
liker (giver of the like) and the
likee (receiver of the like). Every time you create new
like, you should check if the
likee also likes the
If likee.likes.where(likee: liker)? If yes, then create the new connection with both users.
class User < ApplicationRecord has_many :likes has_many :connections end class Like < ApplicationRecord belongs_to :user, :foreign_key => 'liker_id' belongs_to :user, :foreign_key => 'likee_id' end class Connection < ApplicationRecord has_many :likes has_many :users, through: likes end
I want to add that i am not 100% sure of this as i am currently learning Rails myself. But this is what I came up with and hopefully its useful (and correct).