ruby-on-rails - Rails order by 2 columns not working as expected
问题描述
I have match
table which have 2 field match_date
and match_time
. match_date
is of date
data type and match_time
is of time
data type. I want to get all records order by match_date
and match_time
in ascending order. My query looks like this:
tournament = Tournament.find_by(name: 'Test')
matches = tournament.matches.order(:match_date, :match_time)
when I do matches.first.match_time
it's showing match_time
is 11:00 am
. But I also have match with match_time
of 10:40 am
. So I was hoping matches.first.match_time
to return 10:40 am
but I am getting result of 11:00 am
. What am I doing wrong ? My query is definitely not giving me desired output.
UPDATE: I also tried to use sort_by
but I am getting same output. My sort_by query looks like this
matches = tournament.matches.sort_by{ |a| [a.match_date, a.match_time] }
I also don't have any default_scope
in match model.
解决方案
推荐阅读
- sqlite - SQLite 错误:无法在具有非常基本表的事务中启动事务
- python - 我写的这个函数是为了找到素数有效吗
- amazon-web-services - 使用 Postman 删除 DynamoDB 中的项目,获取“缺少所需参数 Id”
- python-3.x - 数据库未在 python 中显示
- java - 在同一方法中调用方法名称?
- python - 将列表拆分为 N 个子列表,总和大致相等
- html - 元素应该在换行符上时移动到 Div 的右侧
- python - 如何从列表中删除列表
- python - 无法为我的 pygame 创建多个敌人
- javascript - 如何以角度从字典对象中排除匹配值?