Open
Conversation
osatoh
requested changes
Mar 5, 2023
| lending = @book.lendings.where(return_status: false, user_id: current_user.id).first | ||
| redirect_to lending if lending | ||
| reservation = @book.reservations.where("reservation_at >= ?", Time.now).where(user_id: current_user.id).first | ||
| @book = Book.eager_load(:reservation_active, :lend_active).where(id: params[:id]).with_attached_image.order("reservations.reservation_at asc").first |
Collaborator
There was a problem hiding this comment.
Suggested change
| @book = Book.eager_load(:reservation_active, :lend_active).where(id: params[:id]).with_attached_image.order("reservations.reservation_at asc").first | |
| @book = Book.eager_load(:reservation_active, :lend_active).find_by(id: params[:id]).with_attached_image.order("reservations.reservation_at asc") |
で取れたりします、、? 👀
Collaborator
Author
There was a problem hiding this comment.
@book = Book.eager_load(:reservation_active, :lend_active).with_attached_image.order("reservations.reservation_at asc").find_by(id: params[:id])
で取得できました。ただ、このやり方だとeager_load とfind_by で2回クエリを発行するみたいです。
今の所は元のコードを使って、他のコントローラーを修正する時に、他の書き方がないか考えてみます
Comment on lines
+26
to
+27
| @user = User.find(current_user.id) | ||
| @lending = @user.lendings.build(lending_params) |
Collaborator
There was a problem hiding this comment.
Suggested change
| @user = User.find(current_user.id) | |
| @lending = @user.lendings.build(lending_params) | |
| @lending = current_user.lendings.build(lending_params) |
で行けるかもです 🙏
Comment on lines
+16
to
+17
| @user = User.find(current_user.id) | ||
| @reservation = @user.reservations.build(reservation_param) |
Collaborator
There was a problem hiding this comment.
Suggested change
| @user = User.find(current_user.id) | |
| @reservation = @user.reservations.build(reservation_param) | |
| @reservation = current_user.reservations.build(reservation_param) |
|
|
||
|
|
||
| <% if @reservations.exists? %> | ||
| <% if @book.reservation_active.any? %> |
Collaborator
There was a problem hiding this comment.
Suggested change
| <% if @book.reservation_active.any? %> | |
| <% if @book.reservation_active %> |
でいい気もします! 👀
Collaborator
Author
There was a problem hiding this comment.
@book.reservation_activeに該当する予約データがない時、その中身は空の配列、[]、になり
<% if @book.reservation_active %> がtrue になってしまいます。 それを回避するために.any?を付けています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
メモ