0

I am trying to add created at for a value in a column for example if I added a new invoice it will show me the date that I add the invoice not the date I add the case

<%= @case.invoice.created_at %>

This gives me null

in my database

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

in my schema

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
nourza
  • 2,215
  • 2
  • 16
  • 42

1 Answers1

0

In the migration for the Invoice do you have t.timestamps?

For example:

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

      t.timestamps
    end
  end
end

If not you may need to add those

class AddTimestampsToInvoice < ActiveRecord::Migration
  def change
    add_column :invoices, :created_at, :datetime, null: false
    add_column :invoices, :updated_at, :datetime, null: false
  end
end
Ben
  • 9,725
  • 6
  • 23
  • 28
  • please check my database AddAttachmentInvoiceToCases how can I fix it ? what should I put in the new migration? I don't have datetime – nourza Jan 26 '23 at 23:42
  • The migration you showed was when you added attachments to the cases ... I mean the migration where you created the invoices. You can use that second migration I put in the answer to add timestamps to the invoices. – Ben Jan 27 '23 at 02:17
  • I am not sure what you are using for attachments. You should try ActiveStorage and see if it helps. It looks like you are hard coding invoices into the Case model which seems like a bad idea. https://guides.rubyonrails.org/active_storage_overview.html – Ben Feb 03 '23 at 03:52
  • the invoice is not a model its just an attribute. its like user.name. I want to do created at for the user.name not for all the user attributes – nourza Feb 10 '23 at 07:30
  • something like at user.name.created_at – nourza Feb 10 '23 at 07:47