First of all, object returned by your bs
method is not an array - it is much more complicated AssociationProxy
object, which wraps an array called internally target
. Your approach leads to extreme case of N+1
problem.
The correct approach is to introduce has_many :through
association:
class A < ActiveRecord::Base
has_many :bs
has_many :cs, through: :bs
end
Then you can just call:
A.cs
Without any changes in db.
If you had more nested association (let's say C has_many :ds), you can get them through another has_many :through association:
class A < ActiveRecord::Base
has_many :bs
has_many :cs, through: :bs
has_many :ds, through: :cs
end