Forum: Ruby on Rails trying to add model inheritance without changing database calls

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.
17198c4d99b317dcdd1b9f881230d34c?d=identicon&s=25 Rynn Steinbolt (rynn)
on 2016-06-11 16:08
Hello Folks!

Im trying to have several model classes inherit from a new class, which
in turn inherits from ActiveRecord::Base. The problem is that my
controller's database calls to the original model classes now look for
the information in the new class's database table, even though it doesnt
exist there.

e.g.
class RawDatum inherits from class ArchModel
RawDataController calls RawDatum.all
generates SQL: "SELECT "arch_models".* FROM "arch_models" ORDER BY
raw_data.timestamp DESC"
correct SQL: "SELECT "raw_data".* FROM "raw_data" ORDER BY
raw_data.timestamp DESC"

The new class is for providing methods that process data from other
models, and to make the corresponding method calls short and simple. It
doesn't need its own database table at all. The desired functionality
already worked before, but the implementation was badly structured.

Does anyone have an idea on how to make this work?
2c1177d893bf3115edf0119cf56e7220?d=identicon&s=25 Greg Navis (Guest)
on 2016-06-13 14:12
(Received via mailing list)
You might try to set `self.table_name = 'raw_data'` in the RawDatum
class
though I'm not sure what it's side-effects would be (if any).

Would the following design work for you?

model ArchModel
  # Include common functionality here.
end

class RawDatum < ActiveRecord::Base
  include ArchModel
end
--
Greg Navis
I help tech companies to scale Heroku-hosted Rails apps.
Free, biweekly scalability newsletter for SaaS CEOs
<http://www.gregnavis.com/newsletter/>
This topic is locked and can not be replied to.