gpt4 book ai didi

mysql - 如何对 'parent' 模型中字段的嵌套项目进行排序?

转载 作者:行者123 更新时间:2023-11-29 20:24:43 25 4
gpt4 key购买 nike

我是 Ruby on Rails 新手,并尝试进行正确的查询。但在阅读文档和示例后,我无法从中得到正确的查询。所以我希望有人能指出我正确的方向。

情况我构建了一个应用程序,培训师可以在其中设置培训,并在多个日期进行这些培训(这些日期称为刺激),其他用户可以订阅这些刺激。它具有以下结构:

class User
has_many :trainings
has_many :thrills, through: :trainings
has_many :reservations

class Training
belongs_to :user
has_many :reservations
has_many :thrills
has_many :users, through: :thrills

class Thrill
belongs_to :training
has_many :reservations
has_many :users, through: :reservations

class Reservation
belongs_to :user
belongs_to :thrill
has_one :training, through: :thrill

问题我怎样才能列出当前用户按激动日期订购的所有预订的列表?惊心动魄的约会设置在 Thrills 模型中。

在我的reservations_controller.rb中,我有:

@reservations = current_user.reservations

这给出了所有预订的正确列表,但我想在reservation.thrill.thrilldate上对此列表进行排序我尝试使用这个 View :

<% @reservations.order("reservation.thrill.thrilldate ASC").each do |reservation| %>

不幸的是,这给出了错误:

SQLite3::SQLException: no such column: reservation..thrill.thrilldate: SELECT "reservations".* FROM "reservations" WHERE "reservations"."user_id" = ? ORDER BY reservation.thrill.thrilldate ASC

谁知道我如何在 Thrills 模型中引用 Thrilldate?谢谢你帮我!

最佳答案

假设 thrilldateThrill 上的一列

@reservations = current_user.reservations.joins(:thrill).order('thrills.thrilldate asc')

关于mysql - 如何对 'parent' 模型中字段的嵌套项目进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39353448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com