-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detect should be replaced by find_by #183
Comments
|
same here
|
|
If I do a find_by I would be doing it for each Hence if there is one return item but 20 order_items, There would be 20 SQL queries here. Yet my current code just does one SQL query. I'm up for changing this pattern but maybe by moving the logic out of the view. |
I know this will create N + 1 queries, but using detect will cause unnecessary data retrieval, either will have some problems. I think removing N + 1 is much more important. |
After iterating through all the Either way I agree I think the N + 1 is more important. Thanks for pointing all this out. You are motivating me to keep adding more things to the repo. |
why not do this in controller and in view implement all necessary data can be loaded beforer view renders |
ror_ecommerce/app/views/admin/rma/return_authorizations/_form.html.erb
<% return_item = @return_authorization.return_items.detect {|p| p.order_item_id == item.id } %>
will issue a query to get all return_items of @return_authorization, and then detect them to find the one with specified item.id.
However, use find_by will only issue the query use where condition
@return_authorization.return_items.find_by(order_item_id: item.id)
The text was updated successfully, but these errors were encountered: