Is there a way to directly reference (using rails directly, without resorting to a lot of custom SQL) a relation that is nested behind a polymorphic relation? In the below example, is there a way to define a has_many relation in User that references LayerTwo?
I'd like to do (in User)
has_many :layer_twos, :through => layer_ones
but this approach doesn't take into the account of the previously specified has_many relations through the polymorphic relation. Any suggestions? It may not be possibly through the existing rails conventions, but figured I'd defer the question to people smarter then myself.
class CreateOwners < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.timestamps
end
create_table :owners do |t|
t.timestamps
t.references :owned, :polymorphic => :true
t.references :user
end
create_table :layer_ones do |t|
end
create_table :layer_twos do |t|
t.references :layer_one
end
end
end
class Owner < ActiveRecord::Base
belongs_to :user
belongs_to :owned, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :owners
has_many :layer_ones, :through => :owners, :source => :owned, :source_type => 'LayerOne'
end
class LayerOne < ActiveRecord::Base
has_many :owners, :as => :owned
has_many :layer_twos
end
class LayerTwo < ActiveRecord::Base
belongs_to :LayerOne
end