Ruby on rails 如何根据类型创建具有不同属性的模型

Ruby on rails 如何根据类型创建具有不同属性的模型,ruby-on-rails,associations,models,Ruby On Rails,Associations,Models,我有一个分类模型 default_scope :order => 'display_order asc' has_many :resources, :dependent => :destroy # == Schema Information # # Table name: categories # # id :integer(4) not null, primary key # name :string(255) # de

我有一个分类模型

   default_scope :order => 'display_order asc'
   has_many :resources, :dependent => :destroy

# == Schema Information
#
# Table name: categories
#
#  id          :integer(4)      not null, primary key
#  name        :string(255)
#  description :string(255)
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
#
我有一个资源模型:

belongs_to :category
  # belongs_to :submitter, :class_name => 'User', :foreign_key => "submitter_id"
  has_and_belongs_to_many :filetypes
  has_many :users, :through => :kits
  has_many :kits
  belongs_to :submitter, class_name: "User"
  belongs_to :author
==架构信息 客户端希望能够创建类别,但根据类别为资源分配不同的属性

例如: 已创建类别:“书籍” 例如,他希望在资源模型中存储字段“author”

已创建类别:“会议” 例如,他希望在资源模型中存储字段“location”、“date”


如何对此进行建模,使其动态且易于长期维护?

一个想法是为类别的字段单独设置一个表。例如,您的设置可能如下所示:

category HABTM category_fields
category_field HABTM categories
category_field has_many category_field_values
category_field_value belongs_to category_field
然后,当您的客户创建一个新类别时,即
书籍
,他可以向该类别添加一个名为
作者
的新类别字段。然后,如果他想在图书类别中添加新作者,他会在
作者
类别字段
中添加一个类别字段值(即JK Rowling)

因此,,
图书类别
将有一个
作者类别字段
,而
作者类别字段
将有一个
类别字段值
(JK Rowling)。

您可以查看此>>

或您考虑使用像MangoDB或ReDIS?/P>的无模式数据库。


您也可以使用postgresql中的键值类型hstore。

谢谢Michael,我会看一下。我很欣赏你的想法。
category HABTM category_fields
category_field HABTM categories
category_field has_many category_field_values
category_field_value belongs_to category_field