我有两个模型,Article和Recipe,它们有一堆相同的属性和方法.我想创建一个新类“Post”的子类,并在那里移动所有共享逻辑,所以我不维护重复的代码.我试过这个:
class Recipe < Post; end class Article < Post; end class Post < ActiveRecord::Base #all the shared logic end
所有这些类都在标准的./app/models文件夹中.但是,当我转到/ articles / new时,此代码会抛出ActiveRecord :: StatementInvalid错误.错误是:
Could not find table ‘posts’
知道如何设置吗?
解决方法
你为什么不使用模块?
module Features def hello p "hello" end end class Recipe < ActiveRecord::Base include Features end class Article < ActiveRecord::Base include Features end Recipe.new.hello # => "hello" Article.new.hello # => "hello"