I am working on a medical application. In it, I have associations between users and clients (obviously), and an association between users and treatments, and users and settings (for a settings table).
But the tricky part here is that the client also has to be associated to the treatments table, because they are the ones that have the treatments on their record. The user is simply the one administrating all of the above. And also we have patients who are associated to clients, so it will need to be multi-layered. So owners have clients who also have patients. And patients have treatments as do Clients and do owners.
It seems really complex to me and I am having a hard time getting anything beyond user working. I am using the basic relationship setup in Rails (i know very little about rails). I am using @treatment.user = current_user on the create method, and then simply associating the tables needed with a user_id. I put a client_id on the treatment table, but that won’t work will it?
Do I need a polymorphic association (not sure what that is though)? OR do I need seperate tables to do all of this matching?
Thanks.
Code:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,
:remember_me, :role
validates :email, presence: true
has_many :clients
has_many :settings
has_many :treatments
ROLES = %w[admin receptionist practitioner]
def role_symbols
[role.to_sym]
end
def to_s
email
end
end
I am not totally sure yet, on what your problem is.
But since you mentioned you are not familiar with Rails, this might help you:
Have a look at the
has_many :through relation(also available ashas_one :through)So you could probably do something like this: