Two models with an association of have_one to have_many possible in Rails?
-
26-10-2019 - |
Question
I am making a rails site where you can schedule an appointment for a service. Currently I have an Appointment and Client model. Now I have decided that in order to get the Client's information (name, email, etc) from an Appointment object, I would make an association between the two models. Now the way I see it, an Appointment should have_one
client and a Client should have_many
appointments. However, I have never seen an association between two model where they both have have_x
associations. So what I am wondering is this is possible, will it work? Also is there a better way to do this, maybe something I'm missing? Any insight would be much appreciated!
Solution
I don't believe that will work. You may want to use the belongs_to
method to associate your Appointment
to your Client
:
class Appointment < ActiveRecord::Base
belongs_to :client
end
class Client < ActiveRecord::Base
has_many :appointments
end
OTHER TIPS
A Client
has_many
:appointments
and an Appointment
belongs_to
a :client