繁体   English   中英

如何在“父”模型中对字段上的嵌套项目进行排序?

[英]How to sort nested items on field in 'parent' model?

我是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

问题我如何列出由thrilldate订购的current_user的所有保留? 刺激物在“刺激物”模型中设置。

在我的Reservations_controller.rb中,我有:

@reservations = current_user.reservations

这给出了所有序列的正确列表,但是我想使用此视图在我尝试这样做的Reservation.thrill.thrilldate上对此列表进行排序:

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

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

SQLite3 :: SQLException:没有这样的列:Reservation..thrill.thrilldate:选择“ reservations”。*从“ reservations”中,在“ reservations”中。“ user_id” =? 预订

谁知道我如何在Thrills模型中提及thrilldate? 谢谢你的协助!

假设thrilldate是有关Thrill的专栏

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM