Sql Ruby Rails-在列中使用创建的_生成表
RubyonRails有t.timestamps方法,它创建了两列,created_at和updated_at。我怎样才能只制作创建的_at列 这很有效Sql Ruby Rails-在列中使用创建的_生成表,sql,ruby-on-rails,ruby,timestamp,Sql,Ruby On Rails,Ruby,Timestamp,RubyonRails有t.timestamps方法,它创建了两列,created_at和updated_at。我怎样才能只制作创建的_at列 这很有效 class CreateLinesSources < ActiveRecord::Migration def change create_table :lines_sources do |t| t.timestamps null: false end end end class CreateLines
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.timestamps null: false
end
end
end
class CreateLinesSources
这两个我都想工作,但都失败了
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.created_at null: false
end
end
end
class CreateLinesSources
及
class CreateLinesSources
就像其他专栏一样。由于列名的原因,Rails仍将负责magic更新。您可以执行以下操作:
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
两次尝试都可以,但都包含语法错误 第一次尝试的问题是t。位于数据类型而不是列名上
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
end
您是否尝试过在一次迁移中添加时间戳,然后在以后的迁移中删除
updated\u
。即使你没有使用它,最糟糕的情况是它会提醒你一个不应该发生的更新。当ActiveRecord
需要它的时候,为什么还要费心删除它呢?节省的空间可以忽略不计。这个答案在我看来要好得多。因为在列中创建updated\u并在不久后将其删除有点奇怪。另外+1用于为时间戳添加Rails行为。
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
class CreateLinesSources < ActiveRecord::Migration
def change
create_table :lines_sources do |t|
t.datetime :created_at, null: false
end
end
end
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