Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在RubyonRails中动态创建具有动态列名的表?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何在RubyonRails中动态创建具有动态列名的表?

Ruby on rails 如何在RubyonRails中动态创建具有动态列名的表?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我需要做的是,我希望能够创建一个具有动态列的表,从散列中检索列名及其数据类型 请以下面的代码段为例: COLUMNS = { :column1 => 'integer', :column2 => 'string', :column3 => 'string', :column4 => 'date' } 以静态方式,我可以这样做: create_

我需要做的是,我希望能够创建一个具有动态列的表,从散列中检索列名及其数据类型

请以下面的代码段为例:

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

不幸的是,它似乎没有按我想要的方式为我工作。

t.integer
正在
t
上调用一个方法,而不是执行数组索引,因此您是否尝试过调用
t.\uuu发送(值,键)