I am having trouble using STI and polymorphism together. I tried
“has_many :as” without success so I am trying to be more explicit.
Here is what I have now
class Visual < ActiveRecord::Base
has_many :tags,
:foreign_key=>‘resource_id’,
:conditions=>[‘resource_type = ?’, self.class.to_s],
:dependent=>:destroy
end
class Picture < Visual
end
=====
I see generated SQL like the following
SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type =
‘Class’))
And I want to see
SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type =
‘Picture’))
How can I adjust the conditions line so that I get Picture not Class?
I am having trouble using STI and polymorphism together. I tried
“has_many :as” without success so I am trying to be more explicit.
Here is what I have now
class Visual < ActiveRecord::Base
has_many :tags,
:foreign_key=>‘resource_id’,
:conditions=>[‘resource_type = ?’, self.class.to_s],
:dependent=>:destroy
end
class Picture < Visual
end
=====
I see generated SQL like the following
SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type =
‘Class’))
And I want to see
SELECT * FROM tags WHERE (tags.resource_id = 1 AND (resource_type =
‘Picture’))
How can I adjust the conditions line so that I get Picture not Class?
This seems to be right up there with “never get involved in a land war
in Asia” as a classic blunder. When you are using STI, the type
information is stored in the table of the STI model. You don’t use a
belongs_to :x, :polymorphic => :true if the X is using STI. Both STI and
:polymorphic => :true allow for polymorphism, but they use separate and
incompatible mechanisms. Choose one or the other and you’ll be fine.
This seems to be right up there with “never get involved in a land war
in Asia” as a classic blunder. When you are using STI, the type
information is stored in the table of the STI model. You don’t use a
belongs_to :x, :polymorphic => :true if the X is using STI. Both STI and
:polymorphic => :true allow for polymorphism, but they use separate and
incompatible mechanisms. Choose one or the other and you’ll be fine.
Hi Josh,
Thanks for the reply.
The belongs_to side of things seems to work just fine unless i haven’t
worked into if far enough yet. It is the has_many side that causes me
the grief.
I thought the following would work regardless of a STI or Polymophic
existance.
Can anyone confirm that what I thought would work below absoultely
will not work? That is a has_many :conditions cannot depend on a
particular instances attributes.
Thanks,
Peter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.