Home>

I'm making a web app with Rails
I'm stumbling in the title, so I'll ask you a question.

In a model called Lessonlesson_dayDate type column and
An integer type that allows you to enter 1 for 1 hour and 2 for 2 hourssetSuppose you have a column called

Also, the Lesson model is
belongs_to: studentIt has become.

By year and month

  1. studentCount so as not to cover the number of
  2. setSum the total time of

I want to ...

If 1,

Lesson
  .where (lesson_day: Date.new (2020, 4, 1) .. Date.new (2021, 3, 31))
  .distinct
  .group ("extract (year from lesson_day)", "extract (month from lesson_day)")
  .order ("extract_year_from_lesson_day")
  .count (: student_id)

And
{[2020.0, 4.0] =>35, [2020.0, 5.0] =>28, [2020.0, 6.0] =>36, [2020.0, 7.0] =>37, [2020.0, 8.0] =>34, [2020.0, 9.0 ] =>27, [2020.0, 10.0] =>31, [2020.0, 11.0] =>38, [2020.0, 12.0] =>38, [2021.0, 1.0] =>36, [2021.0, 2.0] =>33, [2021.0, 3.0] =>39}

In this way, the data with date sorting is obtained.

On the other hand, in the case of 2,

Lesson
  .where (lesson_day: Date.new (2020, 4, 1) .. Date.new (2021, 3, 31))
  .group ("extract (year from lesson_day)", "extract (month from lesson_day)")
  .order ("extract_year_from_lesson_day")
  .sum (: set)

And
{[2020.0, 11.0] =>67, [2020.0, 9.0] =>47, [2020.0, 5.0] =>48, [2020.0, 7.0] =>60, [2020.0, 8.0] =>62, [2020.0, 6.0] ] =>59, [2020.0, 10.0] =>45, [2020.0, 12.0] =>65, [2020.0, 4.0] =>71, [2021.0, 3.0] =>70, [2021.0, 1.0] =>67, [2021.0, 2.0] =>58}

Here, the date sorting is not working.

What could be the problem in this case?

If anyone knows about it, I would appreciate it if you could teach it.

  • Answer # 1

    Date sorting is not working.

    No, as specifiedOnly yearIt is sorted by the condition of.