Control contents of an array sample
up vote
1
down vote
favorite
I have a collection of books. In my view I want to show a random sample of 5 books from the top 10 bestselling books in my DB. However, I only want a maximum of ONE non-fiction title (book.genre) showing in that sample. It's ok if no non-fiction books are sampled.
Controller:
def index
@top_selling_books = Book.all.order('units_sold DESC').first(10).sample(5)
end
View:
<% @top_selling_books.each.with_index(1) do |book, index| %>
<div>
<%= render "books/top_sellers", book: book, index: index %>
</div>
<% end %>
ruby-on-rails ruby
add a comment |
up vote
1
down vote
favorite
I have a collection of books. In my view I want to show a random sample of 5 books from the top 10 bestselling books in my DB. However, I only want a maximum of ONE non-fiction title (book.genre) showing in that sample. It's ok if no non-fiction books are sampled.
Controller:
def index
@top_selling_books = Book.all.order('units_sold DESC').first(10).sample(5)
end
View:
<% @top_selling_books.each.with_index(1) do |book, index| %>
<div>
<%= render "books/top_sellers", book: book, index: index %>
</div>
<% end %>
ruby-on-rails ruby
1
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
1
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
1
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a collection of books. In my view I want to show a random sample of 5 books from the top 10 bestselling books in my DB. However, I only want a maximum of ONE non-fiction title (book.genre) showing in that sample. It's ok if no non-fiction books are sampled.
Controller:
def index
@top_selling_books = Book.all.order('units_sold DESC').first(10).sample(5)
end
View:
<% @top_selling_books.each.with_index(1) do |book, index| %>
<div>
<%= render "books/top_sellers", book: book, index: index %>
</div>
<% end %>
ruby-on-rails ruby
I have a collection of books. In my view I want to show a random sample of 5 books from the top 10 bestselling books in my DB. However, I only want a maximum of ONE non-fiction title (book.genre) showing in that sample. It's ok if no non-fiction books are sampled.
Controller:
def index
@top_selling_books = Book.all.order('units_sold DESC').first(10).sample(5)
end
View:
<% @top_selling_books.each.with_index(1) do |book, index| %>
<div>
<%= render "books/top_sellers", book: book, index: index %>
</div>
<% end %>
ruby-on-rails ruby
ruby-on-rails ruby
asked Nov 14 at 17:28
B B
236138
236138
1
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
1
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
1
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56
add a comment |
1
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
1
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
1
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56
1
1
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
1
1
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
1
1
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If you want to do complicated results, sometimes it's best not to do it all in one query. It can be much harder to maintain than simple logic. However, there is definitely a balance to maintainability, scalability, and performance.
I would suggest doing something like this:
def top_books(limited_genres: )
non_limited_books = Book.where.not(genre: limited_genres).order('units_sold DESC').first(10).sample(5)
limited_books = Book.where(genre: limited_genres).order('units_sold DESC').first(5).sample(1) # rename to limited_book if sample is kept to 1
(top_books + limited_books).sort_by(:units_sold)[0..5]
end
@top_selling_books = top_books(limited_genres: ['non-fiction'])
This is non tested code. It's just to give you an idea on how to accomplish your goals.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If you want to do complicated results, sometimes it's best not to do it all in one query. It can be much harder to maintain than simple logic. However, there is definitely a balance to maintainability, scalability, and performance.
I would suggest doing something like this:
def top_books(limited_genres: )
non_limited_books = Book.where.not(genre: limited_genres).order('units_sold DESC').first(10).sample(5)
limited_books = Book.where(genre: limited_genres).order('units_sold DESC').first(5).sample(1) # rename to limited_book if sample is kept to 1
(top_books + limited_books).sort_by(:units_sold)[0..5]
end
@top_selling_books = top_books(limited_genres: ['non-fiction'])
This is non tested code. It's just to give you an idea on how to accomplish your goals.
add a comment |
up vote
1
down vote
accepted
If you want to do complicated results, sometimes it's best not to do it all in one query. It can be much harder to maintain than simple logic. However, there is definitely a balance to maintainability, scalability, and performance.
I would suggest doing something like this:
def top_books(limited_genres: )
non_limited_books = Book.where.not(genre: limited_genres).order('units_sold DESC').first(10).sample(5)
limited_books = Book.where(genre: limited_genres).order('units_sold DESC').first(5).sample(1) # rename to limited_book if sample is kept to 1
(top_books + limited_books).sort_by(:units_sold)[0..5]
end
@top_selling_books = top_books(limited_genres: ['non-fiction'])
This is non tested code. It's just to give you an idea on how to accomplish your goals.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If you want to do complicated results, sometimes it's best not to do it all in one query. It can be much harder to maintain than simple logic. However, there is definitely a balance to maintainability, scalability, and performance.
I would suggest doing something like this:
def top_books(limited_genres: )
non_limited_books = Book.where.not(genre: limited_genres).order('units_sold DESC').first(10).sample(5)
limited_books = Book.where(genre: limited_genres).order('units_sold DESC').first(5).sample(1) # rename to limited_book if sample is kept to 1
(top_books + limited_books).sort_by(:units_sold)[0..5]
end
@top_selling_books = top_books(limited_genres: ['non-fiction'])
This is non tested code. It's just to give you an idea on how to accomplish your goals.
If you want to do complicated results, sometimes it's best not to do it all in one query. It can be much harder to maintain than simple logic. However, there is definitely a balance to maintainability, scalability, and performance.
I would suggest doing something like this:
def top_books(limited_genres: )
non_limited_books = Book.where.not(genre: limited_genres).order('units_sold DESC').first(10).sample(5)
limited_books = Book.where(genre: limited_genres).order('units_sold DESC').first(5).sample(1) # rename to limited_book if sample is kept to 1
(top_books + limited_books).sort_by(:units_sold)[0..5]
end
@top_selling_books = top_books(limited_genres: ['non-fiction'])
This is non tested code. It's just to give you an idea on how to accomplish your goals.
answered Nov 14 at 18:54
Dbz
1,98142543
1,98142543
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305735%2fcontrol-contents-of-an-array-sample%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What happens if 7 of the top 10 books are non-fiction? Also to satisfy this requirement you are not truly sampling and should probably partition the dataset
– engineersmnky
Nov 14 at 17:40
1
These requirements don't really make sense for the above mentioned edge case. I would suggest changing them to something like: "Show one of the top 3 non-fiction books, and 4 of the top 10 fiction books". Or maybe some other variation like: "Show one the top 10 overall books that's non-fiction (if it exists), and 4-5 of the top 10 fiction books (but not necessarily top 10 overall books), depending on whether that first book was found"
– Tom Lord
Nov 14 at 17:55
1
...In which case, you could just perform two queries and merge the result. Nothing too fancy.
– Tom Lord
Nov 14 at 17:56