# Featured Items Listing

**URL:** <https://www.ruby-forum.com/t/featured-items-listing/87214>\
**Category:** Rails\
**Created:** [February 28, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/featured-items-listing/87214 "2007-02-28T22:56:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jstepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jstepper](https://www.ruby-forum.com/u/jstepper)\
**Post date:** [February 28, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/featured-items-listing/87214/1 "2007-02-28T22:56:29Z")

</div>

Hey All,

Say I have a website with articles and events and I want to create an  
index of featured items which will have a mixture of articles and  
events. I presume I’d create a FeaturedItem model/table with the  
following:

featured\_class \<-- ‘Article’ or ‘Event’  
featured\_id \<-- id of Article or Event  
position \<-- position in featured list

When I pull the featured items from the database, I’m gonna wanna pull  
the corresponding item from the featured\_class table. What’s the best  
way to do this, other than:

if FeaturedItem.featured\_class == ‘Article’  
Article.find()…  
elsif FeaturedItem.featured\_class == ‘Event’  
Event.find()…  
end

Is there anyway to create functionality that will allow me to do  
something like this:

FeaturedItem.featured\_class.find()…

or something similar  
???

---

<div class="post-metadata">

**Author:** ![jstepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jstepper](https://www.ruby-forum.com/u/jstepper)\
**Post date:** [March 1, 2007, 3:03am UTC](https://www.ruby-forum.com/t/featured-items-listing/87214/2 "2007-03-01T03:03:44Z")

</div>

What you want is a polymorphic association between Feature? and both  
Article and Event. Check out the Rails API for ActiveRecord::Base and  
look for the heading “Polymorphic Associations”

---

<div class="post-metadata">

**Author:** ![jstepper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jstepper](https://www.ruby-forum.com/u/jstepper)\
**Post date:** [March 1, 2007, 5:26am UTC](https://www.ruby-forum.com/t/featured-items-listing/87214/3 "2007-03-01T05:26:13Z")

</div>

> the corresponding item from the featured\_class table. What’s the best
> 
> FeaturedItem.featured\_class.find()…

class Article \< ActiveRecord::Base  
has\_one :featured\_thing, :as =\> :featureable  
end

class Event \< ActiveRecord::Base  
has\_one :featured\_thing, :as =\> :featureable  
end

# 

# id :integer

# featureable\_id :integer

# featureable\_type :string

# 

class FeaturedThing \< ActiveRecord::Base  
belongs\_to :featureable, :polymorphic =\> true

def featureable\_thing  
self.featurable\_type.constantize.find\_by\_id(self.featurable\_id)  
end  
end

Maybe?

Then you could do say…

FeaturedThing.find(:all).each do |ft|  
puts ft.featureable\_thing.title # assuming both Articles and Events  
have  
# a title  
end
