scenic-views / scenic

Versioned database views for Rails
https://thoughtbot.com/blog/announcing-scenic--versioned-database-views-for-rails
MIT License
3.43k stars 224 forks source link

Getting "Invalid single-table inheritance type" error when using a model based on a view #361

Closed morphine00 closed 2 years ago

morphine00 commented 2 years ago

Hi, I designed a view that merges records from two tables. When trying to use a model based on that view, depending on what I'm trying to do, I get either

ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: TableA is not a subclass of MergeModel) or ActiveRecord::SubclassNotFound (Invalid single-table inheritance type: TableB is not a subclass of MergeModel)

Here's what the SQL roughly looks like:

( SELECT Custom_ID, Fields_A FROM Table_A
UNION
SELECT Custom_ID, Fields_B FROM Table_B )
ORDER BY custom_order

And here's the model. The "custom_id" field is custom-generated on the view.

class MergeModel < ApplicationRecord

    self.primary_key = :custom_id

    def readonly?
        true
    end
end

Am I doing anything wrong? Any ideas?

morphine00 commented 2 years ago

Addendum:

morphine00 commented 2 years ago

Ok, issue solved: one of my fields in the view was named type, and that's a reserved word in Active Record. That caused AR to think that I was actually querying TableA or TableB, hence the subclass error. Closing.