Jer*_*eng 4 ruby ruby-on-rails
我正在创建一个动态数据库,用户可以在其中创建资源类型,他/她可以在其中添加自定义字段(多个文本,字符串和文件)
每种资源类型都能够显示,导入,导出其数据;
我一直在考虑它,这是我的方法.我很想听听你们的想法.
思路:
只是在数据字段中散列所有自定义数据(亲:写入更容易,con:读回来可能更难);
子字段(模型将有多个字符串字段,文本字段和文件路径字段);
固定数量的自定义字段在同一个表中,其中键映射数据哈希存储在同一行中;
非SQL方法,但问题是动态生成/更改模型以使用不同的自定义字段;
首先,您可以创建几个模型:
- StringData
- BooleanData
- TextData
- FileData
等(您需要的所有数据和字段格式)
每个模型都会参考某个项目,其中包含有关字段的信息
IE:
class Project < ActiveRecord::Base
has_many :project_fields
has_many :string_datas :through => project_fields
has_many :file_datas :through => project_fields
has_many :boolean_datas :through => project_fields
etc ...
end
class ProjectField < ActiveRecord::Base
# title:string field_type:string project_id:integer name:string
belongs_to :project
has_many :string_datas
has_many :file_datas
has_many :boolean_datas
etc ...
end
class StringData < ActiveRecord::Base
# data:string project_field_id:integer
belongs_to :project_field, :conditions => { :field_type => 'String' }
end
class FileData < ActiveRecord::Base
# data:file project_field_id:integer
belongs_to :project_field, :conditions => { :field_type => 'File' }
end
project = Project.new
project.project_fields.new(:title => "Product title", :field_type => "String", :name => 'product_title')
project.project_fields.new(:title => "Product photo", :field_type => "File", :name => 'product_photo')
project.save
<% form_for project do |f| -%>
<% project.project_fields.each do |field| -%>
<%= field_setter field %>
#=> field_setter is a helper method wich creates form element (text_field, text_area, file_field etc) for each type of prject_field
#=> ie: if field.field_type == 'String' it will return
#=> text_field_tag field.name => <input name='product_name' />
<% end -%>
<% end -%>
Run Code Online (Sandbox Code Playgroud)
并创建(更新)方法
def create
project = Project.new(params[:project])
project.project_fields.each do |field|
filed.set_field params[field.name]
# where set_field is model method for setting value depending on field type
end
project.save
end
Run Code Online (Sandbox Code Playgroud)
它没有经过测试和优化,只是展示了实现它的方式.
更新:我已更新代码但它只是模型,你必须认为自己有点:)你可以尝试找出另一个实现
| 归档时间: |
|
| 查看次数: |
3838 次 |
| 最近记录: |