Imagine you have a table with a foreign key, where the relationship must
be fetched not from one other table, but N number of other data sources.
You can do this with:
polymorphic_belongs_to :credit_card do
# Key off of this field to make the switch
group_by(:credit_card_type) do
on(:Visa) # will use VisaResource
on(:Mastercard) # will use MastercardResource
end
end
This can be customized:
polymorphic_belongs_to :credit_card, foreign_key: :cc_id do
group_by(:credit_card_type) do
on(:Visa).belongs_to :visa, resource: SpecialVisaResource do
# all the normal sideload customizations
end
end
end
The low-level API looks like:
resource.sideloads[:credit_card].children[:visa]
A lot of the hijinks here are because some values, like inferring the
foreign key, need to come from the parent. Others need to come from the
child.
BelongsTo sideloads now accept :group_name to support this
The :as option introduced for all sideloads. Let's say the
relationship name is :authors but the model accessor is :user. You
can now do has_many :authors, as: :user.
Imagine you have a table with a foreign key, where the relationship must be fetched not from one other table, but N number of other data sources.
You can do this with:
This can be customized:
The low-level API looks like:
:as
option introduced for all sideloads. Let's say the relationship name is:authors
but the model accessor is:user
. You can now dohas_many :authors, as: :user
.