I currently have an order by working on my Ruby on Rails application so that on the Bookings index a user can order their bookings by show_date
. This is done through the following in the bookings_controller:
def index
@bookings = Booking.joins(:showing).all.order(params.fetch(:sort, 'id asc'))
end
And the view code:
<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>
But what I want to do is enable the user to order by the film's title as well as the show date. I have tried to do this with this code:
views/bookings/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>
And the bookings_controller:
def index
@bookings = Booking.joins(:showing, :film).all.order(params.fetch(:sort, 'id asc'))
end
But this doesn't work and I get the error:
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
I think it is because their isn't an association between bookings and film.
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
Aucun commentaire:
Enregistrer un commentaire