Topic: Inheritence between Active Record models

Using the classic Dog < Animal, if both of these objects are persisted in the database, how do you wire them up together? For example,

class Animal < ActiveRecord::Base
  attr_accessible :sound

class Dog < Animal
  attr_accessible :license

So if I want to get an instance of Dog, I would do "dog = Dog.find(1)", which should contain dog#license=123 and dog#sound="woof". The question I'm having is how does rails / ActiveRecord wire together the correct instances of animal and dog? Does there need to be a belongs_to in dog? Which means I would need an animal_id in dog, which feels more like a "has a" instead of an "is a" relationship.

Last edited by jcblitz (2012-10-31 13:59:57)

Re: Inheritence between Active Record models

So I was able to access Animal from Dog by adding animal_id to Dog, but I cannot access any of the Dog methods, like license. When I do dog#methods, :license does not show up, only the Animal methods. I'm not sure how to proceed.