Ramy
Ramy

Reputation: 21261

get timestamp from datetime value using ActiveRecord

In my RoR app, a table gets created in this way:

class CreateEmailMessages < ActiveRecord::Migration
  def self.up
    create_table :email_messages do |t|
      t.string :recipient
      t.string :subject
      t.text :text
      t.text :html
     t.string :status
      t.belongs_to :owner
      t.text :data
      t.timestamps
    end
  end
...
end

later on, I try to access the created_at field using:

    <td>
      <%= email_message.created_at %>
    </td>

however this only returns a date. how I can get the timestamp?

in the database, the field i'm looking for looks like this:

2011-11-10 19:16:59.056382

i've even added this:

config.active_record.record_timestamps = true

but still no luck. how I can get the timestamp with the date?

Upvotes: 0

Views: 3739

Answers (1)

cvshepherd
cvshepherd

Reputation: 3997

You could try <%= email_message.created_at.to_s(:db) %> or one of the other to_s format options listed here:

http://apidock.com/rails/ActiveSupport/TimeWithZone/to_s#311-Full-List-of-Supported-Formats

Upvotes: 3

Related Questions