Anish Shah
Anish Shah

Reputation: 333

many-to-many: has_many :through and User table relation issue in rails 4

My setup for the models:

class Doctor < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :doctor
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
end

In my application LogedIn-User IS A either doctor,patient or the Admin i got understand that how Doctor and patient relationship work with appointment,But how to setup the user model and table for that

class User_type < ActiveRecord::Base
  belongs_to :doctors, class_name: "USER"
  belongs_to :patients, class_name: "USER"
end

I know that I am missing crucial self association here but how can i do that, or any other way to set up these models and tables for that. Thanks in advance.

Upvotes: 2

Views: 378

Answers (2)

A H K
A H K

Reputation: 1750

class User < ActiveRecord::Base
  has_one :doctor
  has_one :patient
end

class Doctor < ActiveRecord::Base
  belongs_to :user
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  belongs_to :user 
  has_many :appointments
  has_many :physicians, :through => :appointments
end

Hope this will work for you, If you understand many to many relationship properly.

Upvotes: 3

luser
luser

Reputation: 11

You have typo in here: class User_type < ActiveRecord::Base belongs-to :doctors, class_name: "USER" belongs_to :patients, class_name: "USER" end

it should be belongs_to, you sure thats not your problem?

Upvotes: 0

Related Questions