Hello everyone,
I came across with the following case and I was wondering if using a
has_many through association is a valid solution.
Let’s say we have three models: A, B and C. The association between
model A and model B is a 1 to 1 association with B having a Foreign Key
to A. Now, the association between B and C is a 1 to many association
with C having a Foreign Key to B. There’s an action where I need to
create an xml representation of some of the properties of each instance
in a collection of A objects. Additionally, for each instance of A, I
need to append to its serialized representation two properties from
each instance of C. This C instance is associated somehow to the A
instance through an instance of B.
To create the xml for each instance of A, I will need to iterate over
each A instance, for each A instance I will need to read two
attributes of its corresponding C instances. I want to avoid
unnecessary trips to the database, therefore, I would like to load the
A instances together with its somehow corresponding C instances outside
the loop for the xml creation. I thought to add a has_many through
association in A like this:
class A < ActiverRecord::Base
has_many :cs, through: :b
end
so later I could do the following:
as = A.all.includes(:cs)
as.each do |a|
xml.prop_1 a.prop_1
xml.prop_2 a.prop_2
a.cs.each do |c|
xml.prop_3 c.prop_3
xml.prop_4 c.prop_4
end
end
that way, I avoid some unnecessary trips to the database. However, I am
not sure if this approach is correct. According to the Rails
documentation, the examples about the has_many :through association
illustrates its usage when B has a 1 to many association to both A and
C, in other words, a Foreign Key to A and C respectively. The
association in my case between A, B and C is different. So I am not
sure if adding a has_many through in my case would be a valid solution.
I checked the final result and apparently it’s working as expected, but
I would like to know if I am missing something with this approach.
I am going to look at the rails source code to try to find out if this
approach is valid or not, but in the meantime any help to confirm if it
is acceptable to use a has_many through, why and why not, would be
really appreciated.