Show only suppliers with articles in the dropdown-menu for new orders #347

Merged
merged 1 commit into from Feb 18, 2015

Projects

None yet

2 participants

@paroga
Contributor
paroga commented Feb 18, 2015

When there are many suppliers to be able to select them for invoices,
the menu for creating new orders gets unclear.

@wvengen
Member
wvengen commented Feb 18, 2015

Nice idea! Does this generate N+1 queries for the article count? In that case it'd be useful to preload the article count. Or, better, update the controller to only return suppliers that have articles. Could be done using group_by and having.

@paroga
Contributor
paroga commented Feb 18, 2015

Does group_by and having help, when I need to "join" from an additional table?
In SQL I'd do sth like SELECT * FROM suppliers WHERE id in (SELECT supplier_id FROM articles WHERE undeleted)

@wvengen
Member
wvengen commented Feb 18, 2015

Plain SQL would also be an option:

Supplier.where(id: Article.undeleted.select(:supplier_id).distinct)

Using group by and having:

Supplier.joins(:articles).group(:supplier_id).having('COUNT(articles.id) > 0').last

Both materialize to one query. The first was faster for me.

@paroga paroga Show only suppliers with articles in the dropdown-menu for new orders
When there are many suppliers to be able to select them for invoices,
the menu for creating new orders gets unclear.
5b309d3
@wvengen wvengen commented on the diff Feb 18, 2015
app/views/orders/index.html.haml
@@ -6,7 +6,7 @@
= t '.new_order'
%span.caret
%ul.dropdown-menu
- - Supplier.undeleted.order('suppliers.name ASC').each do |supplier|
+ - Supplier.where(id: Article.undeleted.select(:supplier_id).distinct).order('suppliers.name ASC').each do |supplier|
@wvengen
wvengen Feb 18, 2015 Member

Putting these queries in the view is bad practice. I'll move it to the controller when merged.

@wvengen wvengen merged commit 27fe8a4 into foodcoops:master Feb 18, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@paroga paroga deleted the foodcoop1040:suppliers_with_articles branch Feb 18, 2015
@wvengen wvengen modified the milestone: 4.4 Feb 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment