我目前通过处理Ruby on Rails应用程序获得订单,因此在预订索引上,用户可以按show_date
订购预订。这是通过bookings_controller中的以下内容完成的:
def index
@bookings = Booking.joins(:showing).all.order(params.fetch(:sort, 'id asc'))
end
视图代码:
<form>
Order by:
<select name="sort">
<option value="show_date ASC">Show Date (Oldest First)</option>
<option value="show_date DESC">Show Date (Most Recent First)</option>
</select>
<button>
Go
</button>
</form>
但我想做的是让用户按电影的标题和节目日期订购。我试图用这段代码做到这一点:
视图/预订/ index.html.erb:
<form>
Order by:
<select name="sort">
<option value="show_date ASC">Show Date (Oldest First)</option>
<option value="show_date DESC">Show Date (Most Recent First)</option>
<option value="title ASC">Film Title (A-Z)</option>
<option value="title DESC">Film Title (Z-A)</option>
</select>
<button>
Go
</button>
</form>
还有bookings_controller:
def index
@bookings = Booking.joins(:showing, :film).all.order(params.fetch(:sort, 'id asc'))
end
但这不起作用,我收到错误:
ActiveRecord::StatementInvalid in Bookings#index
SQLite3::SQLException: no such column: film:
SELECT "bookings".* FROM "bookings" INNER JOIN "showings" ON
"showings"."id" = "bookings"."showing_id" ORDER BY film ASC
我认为这是因为他们不是预订与电影之间的关联。
Booking.rb:
belongs_to :user
belongs_to :showing
delegate :screen, to: :showing
Showing.rb:
belongs_to :film
has_many :bookings
belongs_to :screen
Film.rb:
has_many :showings
belongs_to :certificate
belongs_to :category
答案 0 :(得分:1)
试试这个:
Booking.joins(showing: :film).whatever
。 这可能会解决嵌套连接问题。