sorting - sort records by two fields in ruby on rails -


there problem (or misunderstanding) sorting of records. in application data set loaded script, data set contains several records, , of them have same created_at field. after that, records edited/updated manually, updated_at different. i'd see non-updated record on top, need sort records 2 fields, first created_at in reverse oder (old data set go bottom), updated_at (already edited records go after non-updated). model looks like:

class part < activerecord::base   default_scope :order => "created_at desc, updated_at" end 

after loading 2 sets of data, try records in console, output looks this:

$ bundle exec rails console loading development environment (rails 3.0.5) >> part.all.each {|p| puts p.created_at} 2011-07-01 19:52:00 utc 2011-07-01 19:52:00 utc 2011-07-01 19:26:44 utc 2011-07-01 19:26:44 utc >> part.all.each {|p| puts p.updated_at} 2011-07-01 19:52:09 utc 2011-07-01 19:52:00 utc 2011-07-01 19:27:15 utc 2011-07-01 19:33:01 utc 

apparently, sorting not work expected, expected (desirable) output is:

>> part.all.each {|p| puts p.updated_at} 2011-07-01 19:52:00 utc 2011-07-01 19:52:09 utc 2011-07-01 19:27:15 utc 2011-07-01 19:33:01 utc 

i tried remove default_scope model, , run following works in console, did not help:

part.group("created_at").order("updated_at").each {|p| puts p.updated_at} part.order("created_at desc, updated_at").each {|p| puts p.updated_at} 

can explain how achieve desired order?

upd: seems, second ordering option not taken account because timestamp stored higher precision seconds, i.e. created_at not 2011-07-01 19:52:00 utc, 2011-07-01 19:52:00.12345 utc. consequently, records have unique created_at timestamp, therefore grouping field not possible. right?

the solution found: instead of sorting 2 fields, ended setting updated_at 0 suggested in ruby on rails: updated_at - set 0 on creation , sorting updated_at field. outputs non-updated records on top.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -