开发者

polymorphic activerecords in rails?

Hi so I have a list of 'areas' that are just areas of my site. I want to be able to insert them in to the database and say what type of area they are, this could be the name of the model that inherits from a base Area. This way I can write a generic controller which uses different views and different logic from the loaded model to decide how the page should act.

Trouble is I have no idea how to do this as I'm new to rails.. Any pointers? (or anyone saying "don't 开发者_如何学运维do it like that! do it like this!" would also be much appreciated)

Thanks


I have just found Single Table Inheritance. This appears like it will solve my problem

for more information read here:

http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/


You'll want to create a column named :type

Rails will automatically use the type column to determine the class of the child class.

Then you can do something like:

class Area < ActiveRecord::Base

end

class UserPage < Area

end

So then when you do

UserPage.create( :key => 'value')

It will create an entry in your areas table with the type field set to UserPage.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜