Mr Mikkél
Mr Mikkél

Reputation: 2643

Rails 3 - table name being pulled from PARENT model?! (ApplicationModel)

I just inherited a project. That project has upgraded to Rails3, and is lacking an application_model. I'm trying to implement it. I've extended all other models from it. The problem is that for some reason, when trying to determine the table name, ActiveRecord is using ApplicationModel, rather than the child model so I'm getting:

Table "your_db.application_models" doesn't exist.

Any thoughts? Why would it be pulling from the app model? Any idea on how to fix?

Thanks!

Upvotes: 1

Views: 100

Answers (1)

Mr Mikkél
Mr Mikkél

Reputation: 2643

Apparently, here's the issue (according to what I found on this page: rails - root model or application model):

ActiveRecord normally detects when you subclass an already subclassed ActiveRecord::Base and uses this to turn STI (single table inheritance) on.
ApplicationModel will be an actual model that is expected to have a table in your database. This can lead to problems down the line.

To fix these two problems, you have to set abstract_class to true for ActiveRecord to properly function.

class ApplicationModel < ActiveRecord::Base self.abstract_class = true end

In contrast to an abstract ActionController, abstract_class must set to true which means the developer must know they cannot remove this line from ApplicationModel. With ApplicationController you can do pretty much whatever you want to it.

Upvotes: 1

Related Questions