在我的 rails 项目中,我有不同的数据列表,我必须通过 CRUD 操作来维护,每个列表都不值得一个模型或整个脚手架来维护它,在 rails 上处理这个的最佳方法是什么?
现在我正在使用带有 name:string content:text 的 List 模型将每个列表保存为列表记录,并在我的应用程序中需要一些列表时进行一些解析。这是我的实际列表模型:
class NoListException < Exception
end
class List < ActiveRecord::Base
validates :name, uniqueness: true
def self.container_types
get_list('container_types').collect do |b|
b.split(',').collect {|c| c.split(':').last }
end.collect {|p| "#{p.last} - #{p.first}" }
end
def self.location_categories
get_id_value_list('location_categories')
end
def self.services_types
get_list('services_types')
end
private
def self.get_id_value_list(name)
get_list(name).collect do |b|
(b.split(',').collect {|c| c.split(':').last }).rotate
end
end
def self.get_list(name)
list = List.find_by_name(name)
raise NoListException if list.nil?
list.content.split(';')
end
end
我认为这是一个非常普遍的问题,因此我问是否有更好的方法来处理这些列表?