简体   繁体   English

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

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

I am new to Ruby on Rails and try to make the right query. 我是Ruby on Rails的新手,请尝试进行正确的查询。 But after reading the documentation and examples I don't manage to get the right query out of it. 但是在阅读了文档和示例之后,我无法从中获得正确的查询。 So I hope someone can point me in the right direction. 所以我希望有人能指出我正确的方向。

Situation I build an app where trainers can setup trainings, and give these trainings on several dates (the dates are called thrills), other users can subscribe for these thrills. 情况我构建了一个应用程序,培训人员可以在其中设置培训,并在多个日期(这些日期称为“刺激”)进行这些培训,其他用户可以订阅这些刺激。 It has the following structure: 它具有以下结构:

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

Question How can I make a list of all reservations of the current_user ordered by the thrilldate? 问题我如何列出由thrilldate订购的current_user的所有保留? The thrilldate is set in the Thrills model. 刺激物在“刺激物”模型中设置。

In my reservations_controller.rb I have: 在我的Reservations_controller.rb中,我有:

@reservations = current_user.reservations

This gives the right list of all the reserations, but I want to sort list this on reservation.thrill.thrilldate I tried so by using this view: 这给出了所有序列的正确列表,但是我想使用此视图在我尝试这样做的Reservation.thrill.thrilldate上对此列表进行排序:

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

Unfortunately this gives the error: 不幸的是,这给出了错误:

SQLite3::SQLException: no such column: reservation..thrill.thrilldate: SELECT "reservations".* FROM "reservations" WHERE "reservations"."user_id" = ? SQLite3 :: SQLException:没有这样的列:Reservation..thrill.thrilldate:选择“ reservations”。*从“ reservations”中,在“ reservations”中。“ user_id” =? ORDER BY reservation.thrill.thrilldate ASC 预订

Who knows how I can refer to the thrilldate in the Thrills model? 谁知道我如何在Thrills模型中提及thrilldate? Thanks for helping me out! 谢谢你的协助!

假设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