0

The timestamp has milliseconds, so if any records are created via automation they will likely have the same seconds value but different millisecond values. I need to do this:

Version.uniq(:created_at)

But, this doesn't work because they are all unique. How can I use to_i, or whatever else might work, to pull this off?

hellion
  • 4,602
  • 6
  • 38
  • 77
  • I've created at class method called created_at_int. Then I tried a query like this: Version.all.uniq(:created_at_int). It did not error...but it did not return the expected results. As a matter of fact...I could type literally anything as the symbol and get the same result. – hellion May 23 '14 at 03:06
  • Try giving `Version.uniq(:created_at).to_time` – Pavan May 23 '14 at 06:19

2 Answers2

1

You'll need the date_trunc() PostgreSQL function:

SELECT DISTINCT date_trunc('second', created_at) FROM "version"

In ruby:

Version.select("date_trunc('second', created_at)").distinct
pozs
  • 34,608
  • 5
  • 57
  • 63
0

To just get rid of fractional seconds, cast to the equivalent type with 0 fractional digits.

SELECT DISTINCT created_at::timestamp(0) FROM "version"

Or timestamptz, you did not disclose your exact type.
For more specific needs use date_trunc().
More details:
Discard millisecond part from timestamp

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • @hellion: I am no Ruby export, but it's just a simple cast (like any other expression). Maybe you need SQL standard notation: `cast(created_at AS timestamp(0))` ... – Erwin Brandstetter May 28 '14 at 18:22