views:

209

answers:

1

Hi all-

I am working with a site that will be pulling down feeds from a lot of different sources, and then saving those streams into a common model, in this case a trait. An example of the code from within the FeedEntry class might be:

feed = Feedzirra::Feed.fetch_and_parse(feed_url)
add_entries(feed.entries) 
...

def self.add_entries(entries)

   entries.each do |entry|  
      # Should know how to parse itself into a trait          
      @trait = parse(entry)
      if @trait.save
      ...
   end  
 end

Admittedly I come from a java background, and in java here, I would set up an inheritance heirarchy, and then on each subclass of FeedEntry extend the parse method so that each FeedEntry knew how to parse itself. So my questions:
1) Is this a feasible plan in rails?
2) If so, would one just include a column that was basically "type" that said what subclass the FeedEntry was?
3) If not, any suggestions on the DRYest way to do this?

Thanks!

+1  A: 

I think you can use Single table inheritance that rails provide . Refer : http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/ and http://api.rubyonrails.org/classes/ActiveRecord/Base.html .

After that you could add the parse method in each one of your "inherited" classes . You might want to add a before_save callback and call self.parse . I am not sure if this is the DRYest way to do this .. would be interesting to see what others say .

NM