Reputation: 93
I was working on a Rails application today (Ruby 2.1.3/Rails 4.1) where there is several classes inheriting from a single class Item:
Main class is Item, there is a Page class that inherits from Item and DownloadPage class that inherits from Page class. All objects are stored in a single table and are distinguished by Type column containing object's class name (Page, Article, DownloadPage, etc.)
Here is the UML:
Here what data looks like in the DB (Item table):
Back to the issue, when application loads, and I go to page#index, all objects of type "Page" from Item table are listed, everything is fine, but when the application uses the class DownloadPage at least once (example: DownloadPage.first), page#index sends every object of Page type PLUS objects of type DownloadPage, which is not normal.
Here is a demo of the issue, notice how SQL query only selects Page type the first time (step 1) and Page+DownloadPage the second time (step 3):
So my question is, Am I doing something wrong, architecture might be bad or something else? Or is that a rails/ruby bug
OUTPUT:
2.1.3 :001 > Page.first
Page Load (0.9ms) SELECT "items".* FROM "items" WHERE "items"."type" IN ('Page')
=> #<Page id: 2, type: "Page",...
2.1.3 :002 > DownloadPage.first
DownloadPage Load (0.6ms) SELECT "items".* FROM "items" WHERE "items"."type" IN ('DownloadPage') AND "items"."deleted_at" IS NULL ORDER BY "items"."id" ASC LIMIT 1
=> #<DownloadPage id: 26, type: "DownloadPage",...
2.1.3 :003 > Page.first
Page Load (1.8ms) SELECT "items".* FROM "items" WHERE "items"."type" IN ('Page', 'DownloadPage')
=> #<Page id: 2, type: "Page", title: "sdfPage"...
Upvotes: 0
Views: 210
Reputation: 1714
class Page < Item
end
require_dependency 'download_page'
https://github.com/rails/rails/issues/8699
Upvotes: 3