Rails中字段名为"date"时无法排序?
Can't order when the field name is "date" in Rails?
我有一个 Game
模型,其中包含字段 start_time
和 date
(以及其他字段)。 @tournament.games.order('start_time asc')
有效,但 @tournament.games.order('date asc')
无效。 t.games.order("date asc")
和 t.games.order("date desc")
产生相同的顺序。这是为什么?
这是我的游戏架构:
create_table "games", force: :cascade do |t|
t.bigint "tournament_id", null: false
t.string "team_one_type", null: false
t.bigint "team_one_id", null: false
t.string "team_two_type", null: false
t.bigint "team_two_id", null: false
t.bigint "field_id", null: false
t.date "date"
t.datetime "start_time"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["field_id"], name: "index_games_on_field_id"
t.index ["team_one_type", "team_one_id"], name: "index_games_on_team_one_type_and_team_one_id"
t.index ["team_two_type", "team_two_id"], name: "index_games_on_team_two_type_and_team_two_id"
t.index ["tournament_id"], name: "index_games_on_tournament_id"
end
这可能只是因为你只按日期搜索(相同日期的记录不会改变),但是,如果你按日期时间排序,每条记录都会排序(包括相同日期的记录)。
我有一个 Game
模型,其中包含字段 start_time
和 date
(以及其他字段)。 @tournament.games.order('start_time asc')
有效,但 @tournament.games.order('date asc')
无效。 t.games.order("date asc")
和 t.games.order("date desc")
产生相同的顺序。这是为什么?
这是我的游戏架构:
create_table "games", force: :cascade do |t|
t.bigint "tournament_id", null: false
t.string "team_one_type", null: false
t.bigint "team_one_id", null: false
t.string "team_two_type", null: false
t.bigint "team_two_id", null: false
t.bigint "field_id", null: false
t.date "date"
t.datetime "start_time"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["field_id"], name: "index_games_on_field_id"
t.index ["team_one_type", "team_one_id"], name: "index_games_on_team_one_type_and_team_one_id"
t.index ["team_two_type", "team_two_id"], name: "index_games_on_team_two_type_and_team_two_id"
t.index ["tournament_id"], name: "index_games_on_tournament_id"
end
这可能只是因为你只按日期搜索(相同日期的记录不会改变),但是,如果你按日期时间排序,每条记录都会排序(包括相同日期的记录)。