ruby-on-rails rails为列值添加created_at

3okqufwl  于 2023-01-27  发布在  Ruby
关注(0)|答案(1)|浏览(160)

我正在尝试为列中的值添加created at,例如,如果我添加了新发票,它将显示添加发票的日期,而不是添加案例的日期

<%= @case.invoice.created_at %>

这会得到null
在我数据库中

class AddAttachmentInvoiceToCases < ActiveRecord::Migration
  def self.up
    change_table :cases do |t|
      t.attachment :invoice
    end
  end

  def self.down
    remove_attachment :cases, :invoice
  end
end

在我架构中

create_table "cases", force: :cascade do |t|
    t.string   "pt_first_name"
    t.string   "pt_last_name"
    t.date     "date_received"
    t.date     "due_date"
    t.string   "shade"
    t.string   "mould"
    t.string   "upper_lower"
    t.integer  "user_id"
    t.string   "invoice_file_name"
    t.string   "invoice_content_type"
    t.integer  "invoice_file_size",     limit: 8
    t.datetime "invoice_updated_at"
    t.string   "implant_brand"
    t.string   "implant_quantity"
    t.integer  "number"
    t.boolean  "finished"
    t.boolean  "ship"
    t.boolean  "outsourced"
    t.string   "invoice2_file_name"
    t.string   "invoice2_content_type"
    t.integer  "invoice2_file_size",    limit: 8
    t.datetime "invoice2_updated_at"
    t.string   "invoice3_file_name"
    t.string   "invoice3_content_type"
    t.integer  "invoice3_file_size",    limit: 8
    t.datetime "invoice3_updated_at"
    t.string   "invoice4_file_name"
    t.string   "invoice4_content_type"
    t.integer  "invoice4_file_size",    limit: 8
    t.datetime "invoice4_updated_at"
    t.string   "invoice5_file_name"
    t.string   "invoice5_content_type"
    t.integer  "invoice5_file_size",    limit: 8
    t.datetime "invoice5_updated_at"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
anauzrmj

anauzrmj1#

Invoice的迁移中,您是否有t.timestamps
例如:

class CreateInvoices < ActiveRecord::Migration
  def change
    create_table :invoices do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end

否则,您可能需要添加这些内容

class AddTimestampsToInvoice < ActiveRecord::Migration
  def change
    add_column :invoices, :created_at, :datetime, null: false
    add_column :invoices, :updated_at, :datetime, null: false
  end
end

相关问题