This is My Models.
<code>class Patient < ApplicationRecord
has_many :patient_segments
has_many :segments, through: :patient_segments
end
class Segment < ApplicationRecord
has_many :patient_segments
has_many :patients, through: :patient_segments
end
class PatientSegment < ApplicationRecord
belongs_to :patient
belongs_to :segment
end
</code>
<code>class Patient < ApplicationRecord
has_many :patient_segments
has_many :segments, through: :patient_segments
end
class Segment < ApplicationRecord
has_many :patient_segments
has_many :patients, through: :patient_segments
end
class PatientSegment < ApplicationRecord
belongs_to :patient
belongs_to :segment
end
</code>
class Patient < ApplicationRecord
has_many :patient_segments
has_many :segments, through: :patient_segments
end
class Segment < ApplicationRecord
has_many :patient_segments
has_many :patients, through: :patient_segments
end
class PatientSegment < ApplicationRecord
belongs_to :patient
belongs_to :segment
end
So, Patient has many segments and also Segment has many Patients.
and I made some PatientSerializer and I think there is two way to include segment in PatientSerializer
First Way
<code>class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description
has_many :segments
end
</code>
<code>class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description
has_many :segments
end
</code>
class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description
has_many :segments
end
Second Way
<code>class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description,
:segments
end
</code>
<code>class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description,
:segments
end
</code>
class PatientSerializer < ActiveModel::Serializer
attributes :id,
:name,
:description,
:segments
end
I think both of these approaches work well,
but I’m not sure which one to use.
Are there any differences between the two methods, and is one preferred over the other?