ruby on rails - Check and select the latest one from the array of hashes -


i have check , select latest 1 array of hashes. structure this:

'histories':[ {   {    ...   },   'created': "date1",   'items':[     {      'a': "ready",      'b': "dfknsknfs",     },     {      'a': "sdfjbsf",      'b': "hello23",     }   ] }, {   {    ...   },   'created': "date2",   'items':[     {      'a': "sknfkssd",      'b': "ksdfjshs",     },     {      'a': "ready",      'b': "shdfjsh",     }   ] }, ... ] 

i have first find value "ready" , have select latest "created" date.

my attempt like

ready_item = histories.select { |item| item.items.detect {|f| f.a == "ready" } } ready_item 

but since detect used, returning first detected value. need latest date. should possible solution of it?

histories.select { |h|    h[:items].detect {|f| f[:a] == 'ready' }  }.sort_by {|x| x[:created_at] }.last 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -