I would like a relationship along the lines of
has_many :foos, :through => :multiple
models
Site:
has_many :pages
has_many :files
Page:
belongs_to :site
has_many :legacy_url_redirects, as: :redirect_resourceable
File:
belongs_to :site
has_many :legacy_url_redirects, as: :redirect_resourceable
LegacyUrlRedirects
belongs_to :redirect_resourceable, :polymorphic => true
What I would like to do is in the Site
model add something like:
has_many :legacy_url_redirects, :through => [:pages, :files]
Or an equally good alternative.
Thanks.
EDIT:
Here is what i did: Site model:
def legacy_url_redirects(options = {})
file_where = {"files.site_id" => self.id}.merge(options)
page_where = {"pages.site_id" => self.id}.merge(options)
LegacyUrlRedirect.includes(:file).where(file_where) + LegacyUrlRedirect.includes(:page).where(page_where)
end
I also added the :pages
and :files
relationships to the LegacyUrlRedirects
model with help from: https://stackoverflow.com/a/16124295/1141264
SECOND EDIT:
I refactored the fix a little so that its a manually written LEFT OUTER JOIN
between the three tables. Also not specifying the select part of the query when doing a join implies @readonly = true
on the records returned.