0
我需要做的是我想能够创建一个动态列的表,从列表中检索列名和数据类型。如何在ruby-on-rails中动态创建带有动态列名的表?
请把下面的代码片段例如:
COLUMNS = { :column1 => 'integer',
:column2 => 'string',
:column3 => 'string',
:column4 => 'date'
}
在静态的方式,我可以这样做:
create_table :details do |t|
t.integer column1
t.string column2
t.string column3
t.date column4
t.timestamps
end
但是,你知道,这看起来有点硬编码,我对此并不满意。
我的想法是让它看起来更像:
create_table :details do |t|
COLUMNS.each_pair do |key,value|
#to define each column and its data type
t[value] key
end
t.timestamps
end
不幸的是,它似乎并没有为我工作,我想要的方式。