I'm new to Ruby and Rails (and programming!), and trying to figure out the idiomatic way of passing properties from a model to its STI children.
I have a generic model 'Document', and some models that inherit from it — let's take 'Tutorial' as the example. I have a string field for an 'icon', in which I want to store the icon's filename but not full path (I think the path should be up to each model, as it's a detail of retrieving the record's data?):
class Document < ActiveRecord::Base
attr_accessible :title, :body, :icon
@@asset_domain = "http://assets.example.com/"
@@asset_path = "documents/"
def icon
@@asset_domain.to_s + @@asset_path.to_s + read_attribute(:icon).to_s
end
end
This is the kind of thing I'd like to do with the subclasses, so they look for their 'icons' (or any other asset) in an appropriate place.
class Tutorial < Document
attr_accessible :title, :body, :icon
@@asset_path = "tutorials/"
# Other tutorial-only stuff
end
I've read about class variables and understand why what I've written above didn't work quite as I intended, but what's the best way of overriding 'asset_path' in the Tutorial class? I don't think I should use instance variables as the values don't need to change per instance of the model. Any ideas much appreciated (even if it means rethinking it!)