Sti + namespace

I’m spidering historical weather data from various sources. I have a
WeatherStation class that is begging for an STI implementation: a
WeatherStation is associated with a specific weather service, and each
weather service has its own protocol for fetching weather data, but most
of the code is can be shared in the WeatherStation parent class. Ergo
STI.

But there are a lot of weather services, so I’d like to gather all the
subclasses into a subdirectory (that’s aka namespace, right?).

The table will be something like:

create_table :weather_stations do |t}
t.string :callsign
t.string :type # for STI
t.timestamp attempted_at # be nice to webmasters
t.timestamp succeeded_at
end

… and my directory structure is:

apps/models/WeatherStation.rb
apps/models/weather_stations/noaa.rb
apps/models/weather_stations/cwop.rb
apps/models/weather_stations/wolfram.rb

… etc.

So: are there any special considerations in this approach? The docs
talk about namespacing models, and talk (briefly) about STI, but not
together. Do I have to do anything special with table_name_prefix or
descends_from_active_record? ?

I’ll set about trying this out as soon as I hit [submit], but I’d
appreciate a heads up if I’m headed for trouble!

TIA.

  • ff

I know this is an old topic, and perhaps you’ve solved it. But for
anyone who comes across this thread, here’s a post that might be
helpful:

Francis P. wrote in post #1052528:

I know this is an old topic, and perhaps you’ve solved it.

@Francis: thank you for that.

For the record, what I’ve ended up doing is something like the
following. The key thing that took me a while to figure out what to set
the table_name in the base class:

Define a module WeatherStation:

file: app/models/weather_station.rb

module WeatherStation
require ‘weather_station/base’ # need this first
Dir[Rails.root.join(“app/models/weather_station/**/*.rb”).to_s].each
{|f| require f}
end

Define a base class. Note the setting of table_name – it simplifies
things!

file: app/models/weather_station/base.rb

module WeatherStation
class Base
self.table_name = ‘weather_stations’

end
end

Then each weather station inherits from base:

file: app/models/weather_station/noaa.rb

module WeatherStation
class NOAA < Base

end
end