简体   繁体   中英

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.

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM