Help Naming a Model in Rails
I have a patient model and a client model. A patient can have many clients and a client can have many patients. I want to create a model for the association. What do I call it?
A client is a hospital or doctor's office. A Patient is someone who needs education from a开发者_开发百科 hospital or doctors office.
What's a client? Is it a doctor of some kind?
How about registrations
or enrollments
?
You'd then have:
# client
has_many :enrollments
has_many :patients, :through => :enrollments
# patient
has_many :enrollments
has_many :clients, :through => :enrollments
If you want to follow the conventions, then the names in the join table just need to be sorted alphabetically:
# create_clients_patients.rb
create_table "clients_patients", :id => false do |t|
t.column "client_id", :integer, :null => false
t.column "patient_id", :integer, :null => false
end
If you need access to a model class you probably don't have to use a many-to-many association but a:
has_many :through
association which you can name as you prefer.
The many-to-many association has only table naming convention (in your example clients_patients) because you don't need to access the cross table model directly.
精彩评论