Rails 5: group records by “DateTime” field in tree view

為{幸葍}努か 提交于 2019-12-24 07:15:14

问题


I'm struggling to implement tree view. I have a table where there is DateTime field. I need is to create a view something like this where records are group by that single DateTime field values:

-Year
  -Month
    -Day
      /All records belonging to particular day is populated.../
     ...

There can be multiple years, months, days so records have to be grouped accordingly. For record grouping purposes I could use Groupdate gem.

Any hint on how to build something like this in controller/model would be great. Thank you!

Update

In Index action I have dropdown where on Media select, media_id is passed to find its placements. In placements I have :adbreaks column which is DateTime and by which I'd like to do grouping. In adbreaks there can be loaded any random datetime's and my app has to group by year, month, date dynamically.

/manage_inventory_controller.rb

def index
  @media = Media::Media.where(company_id: user_companies)
  end

  def update_placements
  @placements = Placements::Radios.joins(positions: :media).where('media_id = ?', params[:media_id])
  respond_to do |format|
    format.js
  end
  end

回答1:


samples = 100.times.map { |n| { id: n, time: rand(10000).hours.ago } }

data = samples.group_by { |rec| rec[:time].to_date }.group_by { |d, _| d.at_beginning_of_month }.group_by { |m, _| m.at_beginning_of_year }

data.sort_by(&:first).each { |y, recs| puts "-#{y.year}"; recs.sort_by(&:first).each { |m, recs| puts "  -#{m.strftime '%B'}"; recs.sort_by(&:first).each { |d, recs| puts "    -#{d.day}"; recs.each { |rec| puts "      id: #{rec[:id]}" } } } }

it outputs:

-2015
  -November
    -13
      id: 32
    -16
      id: 41
    -19
      id: 4
    -20
      id: 39
    -21
      id: 86
  -December
    -4
      id: 68
    -7
      id: 35
    -8
      id: 98
    -22
      id: 10
    -27
      id: 77
-2016
  -January
    -10
      id: 37
    -19
      id: 67
    -31
      id: 85
  -February
    -2
      id: 78
    -13
      id: 89
    -16
      id: 75
    -17
      id: 36
      id: 99
    -22
      id: 28
  -March
    -9
      id: 7
    -11
      id: 66
    -16
      id: 59
    -26
      id: 18
    -29
      id: 8
  -April
    -2
      id: 3
    -8
      id: 69
    -9
      id: 60
    -11
      id: 2
    -12
      id: 33
    -15
      id: 26
    -17
      id: 17
      id: 46
    -19
      id: 13
    -22
      id: 12
    -24
      id: 64
    -27
      id: 50
    -29
      id: 14
    -30
      id: 43
  -May
    -25
      id: 82
    -26
      id: 11
  -June
    -3
      id: 72
    -8
      id: 92
    -14
      id: 25
    -19
      id: 15
    -22
      id: 95
    -23
      id: 56
    -26
      id: 80
    -27
      id: 87
    -29
      id: 19
  -July
    -1
      id: 6
    -2
      id: 81
    -3
      id: 1
    -6
      id: 88
    -7
      id: 65
    -15
      id: 91
    -17
      id: 27
    -20
      id: 38
    -26
      id: 73
  -August
    -1
      id: 0
    -7
      id: 30
    -8
      id: 79
    -15
      id: 9
    -23
      id: 24
    -25
      id: 21
  -September
    -1
      id: 52
    -7
      id: 48
    -9
      id: 34
    -10
      id: 54
      id: 55
    -22
      id: 70
    -27
      id: 23
    -29
      id: 94
  -October
    -2
      id: 22
    -7
      id: 51
    -23
      id: 63
    -24
      id: 57
      id: 62
    -28
      id: 58
      id: 76
    -29
      id: 90
    -31
      id: 49
  -November
    -5
      id: 5
      id: 45
    -6
      id: 61
    -9
      id: 93
    -12
      id: 83
    -13
      id: 71
    -16
      id: 53
    -17
      id: 29
    -21
      id: 44
    -23
      id: 84
  -December
    -9
      id: 31
    -13
      id: 16
    -14
      id: 47
    -16
      id: 42
    -17
      id: 20
      id: 96
    -18
      id: 97
    -19
      id: 74
    -23
      id: 40


来源:https://stackoverflow.com/questions/41349588/rails-5-group-records-by-datetime-field-in-tree-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!