| id | user_id | created_at (datetime) |
| 1 | 1 | 17 May 2016 10:31:34 |
| 2 | 1 | 17 May 2016 12:41:54 |
| 3 | 2 | 18 May 2016 01:13:57 |
| 4 | 1 | 19 May 2016 07:21:24 |
| 5 | 2 | 20 May 2016 11:23:21 |
| 6 | 1 | 21 May 2016 03:41:29 |
Run Code Online (Sandbox Code Playgroud)
如何获得唯一且最新的created_at user_id记录的结果,在上述情况下将是记录ID 5和6?
到目前为止我尝试过的
到目前为止,我试图使用group_by返回这样的哈希:
Table.all.group_by(&:user_id)
#{1 => [record 1, record 2, …Run Code Online (Sandbox Code Playgroud) 我有一个在webpack js中呈现的日历模块- app/javascript/packs/application.js
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new Calendar(calendarEl, {
plugins: [ interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin, momentPlugin ],
header: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
},
defaultDate: '2018-01-12',
navLinks: true, // can click day/week names to navigate views
editable: true,
eventLimit: true, // allow "more" link when too many events
selectable: true,
events: '/events.json',
select: function(info) {
$.getScript('/events/new', function(){
$('#event_date_range').val(moment(info.start).format('YYYY-MM-DD HH:mm') + ' - ' + moment(info.end).format('YYYY-MM-DD HH:mm'));
$('#start_date').val(moment(info.start).format('YYYY-MM-DD HH:mm'));
$('#end_date').val(moment(info.end).format('YYYY-MM-DD HH:mm')); …Run Code Online (Sandbox Code Playgroud) 我使用 nokogiri 将 xml 文档解析为哈希数组:
助手/国家.helper
module CountriesHelper
def parse
@countries = ['australia', 'canada', 'france']
@countries.inject([]) do |memo, country|
File.open("public/#{country}.xml") do |f|
xml = Nokogiri::XML(f)
path = "//country/stores/"
memo << xml.xpath(path).map do |x|
{ 'country' => x.parent['country'],
'store' => x['store']}
end
end
end
# [{"country"=>"australia", "store"=>"store1"}, {"country"=>"france", "store"=>"store2"}]
Run Code Online (Sandbox Code Playgroud)
如何将这个哈希格式数组保存到我的数据库中?假设我有两个模型 Country 和 Store。
我有这个数组:
a = [[1,2,3,4,5],[3,5,6,8,12,45],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]]
Run Code Online (Sandbox Code Playgroud)
我想合并它的内部数组,使它们成为:
a = [[1,2,3,4,5,3,5,6,8,12,45],[3,2,1,5,7,9,10,11,3,5,6,8,2,1,3,4,6]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?