Ruby on rails has the t.timestamps method which creates two columns, created_at and updated_at. How can I make just the created_at column?
Ruby on rails具有t.timestamps方法,该方法创建两列,created_at和updated_at。我怎样才能创建created_at列?
This works
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.timestamps null: false
end
end
end
Both of these I want to work but fail
这两个我想工作但失败了
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.created_at null: false
end
end
end
and
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.timestamps null: false
remove_column updated_at
end
end
end
3 个解决方案
#1
5
Both attempts are okay but contains syntax error.
两次尝试都没问题,但包含语法错误。
The problem on the first attempt is that the t. is on datatype not column name.
第一次尝试的问题是t。是基于数据类型而不是列名。
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
end
The problem on the second attempt is that remove_column
does not work inside create_table
block. You can move it outside the block and provide correct syntax to remove the column.
第二次尝试的问题是remove_column在create_table块中不起作用。您可以将其移出块外,并提供正确的语法来删除列。
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.timestamps null: false
end
remove_column :line_sources, :updated_at, :datetime
end
end
#2
1
t.datetime :created_at, null: false
Just like any other column. Rails will still take care of magic updating because of column name.
就像任何其他专栏一样。由于列名,Rails仍然会处理魔术更新。
#3
1
You can do:
你可以做:
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
#1
5
Both attempts are okay but contains syntax error.
两次尝试都没问题,但包含语法错误。
The problem on the first attempt is that the t. is on datatype not column name.
第一次尝试的问题是t。是基于数据类型而不是列名。
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
end
The problem on the second attempt is that remove_column
does not work inside create_table
block. You can move it outside the block and provide correct syntax to remove the column.
第二次尝试的问题是remove_column在create_table块中不起作用。您可以将其移出块外,并提供正确的语法来删除列。
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.timestamps null: false
end
remove_column :line_sources, :updated_at, :datetime
end
end
#2
1
t.datetime :created_at, null: false
Just like any other column. Rails will still take care of magic updating because of column name.
就像任何其他专栏一样。由于列名,Rails仍然会处理魔术更新。
#3
1
You can do:
你可以做:
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end