STI with Polymorphism trouble

Hi,

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?

Thanks,
Peter

Peter M. wrote:

Hi,

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.


Josh S.
http://blog.hasmanythrough.com

On 8/31/06, Josh S. [email protected] wrote:

       :foreign_key=>'resource_id',

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.

has_many :tags,
:foreign_key=>‘resource_id’,
:conditions=>[‘resource_type = ?’, self.class.to_s],
:dependent=>:destroy

I thought in the :conditions of the has_many there would be a way to
use the attributes of the model that actually has_many. Is that
possible?

Other things like

:conditions=>[‘blahblah = ?’, self.id],
:conditions=>[‘random_attribute = ?’, self.foo],

Thanks again,
Peter

Hi,

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