开发者

In Ruby on Rails, if we generated a model "Animal", and now want to have "Dog", how should we do it?

开发者 https://www.devze.com 2023-01-14 20:46 出处:网络
say, if we generated a model rails generate model animal name:string birthday:date and now we want to create other model to inherit from it (such as Dog and Cat), should we use rails generate model

say, if we generated a model

rails generate model animal name:string birthday:date

and now we want to create other model to inherit from it (such as Dog and Cat), should we use rails generate model again or just add the files ourselves? How do we specify Dog should inherit from Animal if we use rails generate model?

I think if we use rails generate model instead of adding the model files ourselves, there will be unit test files and fixture files created for us as well. A migration file is also added, except if it is using MongoDB, then there will be no migration fi开发者_C百科le.


If the Dog, Cat and other subclasses you are planning are not going to diverge away from Animal model, you can STI (Single Table Inheritance) pattern here.

To do that, add a String column to Animal. And then you can have:

class Dog < Animal
end

class Cat < Animal
end

>> scooby = Dog.create(:name => 'Scooby', :date => scoobys_birthdate)
=> #<Dog id: 42, date: "YYYY-MM-DD", type: "Dog">

To generate model Dog

$ script/generate model Dog --skip-migration

And then change (usually app/models/dog.rb):

class Dog < ActiveRecord::Base

to

class Dog < Animal


As far as I know you can't specify a superclass when generating a model. However, generators are only a stepping stone to creating your classes. You can generate the model class as normal and simply change the superclass in the generated model file. There are no other places that the inheritance relationship has to be specified for the generated files to work (the fixtures and unit tests for example don't specify super- or subclasses).

So:

script/generate model Dog

Then change:

class Dog < ActiveRecord::Base

to:

class Dog < Animal

If you want to generate a model that will inherit from Animal using single table inheritance then you may want to specify --skip-migrations on the script/generate call (though you may want a migration to add e.g. dog-specific columns to the animals table and you will need to add a type column of type string to the animals table).

0

精彩评论

暂无评论...
验证码 换一张
取 消