Forum: Ruby on Rails Make a STI-compatible finder for a polymorphic column ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nicolas B. (Guest)
on 2008-11-03 15:45
Hi,

I've got a model with a polymorphic column and STI models.
I've added a finder to search for specific types.
How can I search for a specific model that is STI because Rails stores
the mother class name ?

class Activity < ActiveRecord::Base
  belongs_to :item, :polymorphic => true

  named_scope :filter_items, lambda { |i| { :conditions => { :item_type
=> i } } }
end

class Person < ActiveRecord::Base
end

class Site < ActiveRecord::Base
end

class Event < Site
end

class Place < Site
end

Activity.filter_items('Person') works but Activity.filter_items('Event')
and Activity.filter_items('Place') don't work because Rails stores
'Site' in item_type column...

I would like to be able to search through STI and non-STI models...

Thanks for your answers,
Nicolas.
This topic is locked and can not be replied to.