I have a category model
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
#
I have a resources model:
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
== Schema Information
#
# Table name: resources
#
# id :integer not null, primary key
# title :string(255)
# url :string(255)
# description :string(255)
# price :decimal(, )
# created_at :datetime not null
# updated_at :datetime not null
# category_id :integer
# image_file_name :string(255)
# image_content_type :string(255)
# image_file_size :integer
# image_updated_at :datetime
# status :string(255)
# submitter_id :integer
# author_id :integer
#
The client wants to be able to create categories but assign different attributes to the resource depending on the category.
Example:
category created: ‘Books’
He would like to have fields ‘author’ for example stored in the Resource model.
category created: ‘Conference’
He would like to have fields ‘location’, ‘date’ for example stored in the Resource model.
How can I model this so its dynamic and easy to maintain in the long term?
One idea would be to have a separate table just for the fields of a category. For example, your setup could look something like this:
Then, when your client created a new category, i.e.
Books, he could add a new category_field to that category calledauthor. Then if he wanted to add a new author to the Books category, he would add a category_field_value (i.e. JK Rowling) to theauthorcategory_field.Thus,
the
Books categorywould have anauthor category_field, and theauthor category_fieldwould have onecategory_field_value(JK Rowling).