Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing models in Rails

I have two models, Article and Recipe, which have a bunch of the same attributes and methods. I want to make the subclasses of a new class "Post" and move all their shared logic in there so I'm not maintaining duplicate code. I've tried this:

class Recipe < Post; end
class Article < Post; end
class Post < ActiveRecord::Base
     #all the shared logic
end

All of these classes are in the standard ./app/models folder. This code, however, throws a ActiveRecord::StatementInvalid error when I go to /articles/new, for instance. The error is:

Could not find table 'posts'

Any idea how to set this up?

like image 918
weotch Avatar asked Jan 31 '11 15:01

weotch


People also ask

What is STI in Rails?

Single-table inheritance (STI) is the practice of storing multiple types of values in the same table, where each record includes a field indicating its type, and the table includes a column for every field of all the types it stores.

How does inheritance work in Rails?

Inheritance is when a class receives or inherits the attributes and behavior of another class. The class that is inheriting the behavior is called the subclass (or derived class) and the class it inherits from is called the superclass (or base class). Imagine several classes - Cat, Dog, Rabbit, and so on.

What is abstract class Rails?

Abstract classes allow for something that resembles a true interface object in Rails: the model produces holds behavior that's common to all of its children, but — because it has no data representation — it holds (and knows nothing of) the data required by its children.

What is polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.


2 Answers

Rails is using Single Table Inhritance pattern by default (just google for it), so when you're subclassing a model, all the resulting models will use the same database table (in this case posts). You can put all your common methods and validations in the Post model, and specific ones in the other classes, but all those classes will have access to each other's fields, because they share the same table (that's not a big problem though).

If you just want to share code (methods), you'd be better off just putting some common methods into a module in a file in the lib directory and including it in each model. Or you could put the module definition at the top if you're keeping all the models in a single file like in your example.

like image 72
Matt Avatar answered Sep 25 '22 05:09

Matt


Why don't you use modules?

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"
like image 33
Simone Carletti Avatar answered Sep 22 '22 05:09

Simone Carletti