Temporarily skipping ActiveRecord record based on condition

I’ve got an ActiveRecord collection. As I’m dumping rows onto screen each record has a known width. I’m experiencing some rows with leftover space at the end.

Each record either has a value of 2 or 4. I can easily keep track of count, but for instance if I have a ‘count’ of 10 and the next record is a value of 4…*can I ‘skip’ it, check the next record for a value of 2 and then come back to ‘skipped’ records?

Would I have to have an array that I could dump into and when I got to a record that fulfilled my value go back…*output the skipped records and then continue on with the recordset? Seems like I’m probably creating more work this way, but the view could be cached. This isn’t something that would change THAT often.

Thanks for help, hints, etc.

I have no idea what you’re trying to do :slight_smile:

With loops in views you could keep track of the previous value but it’s probably best to do any data wrangling in the controller before it gets to the view to avoid spaghetti.

<% @previous_row = nil %>
<% @rows.each do |row|%>

  <% @previous_row = row %>
<% end %>

I’ve got this File management engine that I’m playing with. The icons either have a portrait or landscape class associated with them. Imagine a grid where an unordered list has li elements with either class. In a 12 column grid, landscape images take up 4 columns, portrait take up 2. Right now they’re floating left, making up rows and a ‘justified’ layout. However if 5 portrait files happen in a row (2*5 = 10) and then a landscape file happens (4), it gets bumped to the next row…*leaving whitespace at the end of the previous ‘row’.

I would like to try to eliminate that space by skipping to the next record till I hit a portrait value, then laying out what I’ve collected (landscape records), and reentering the normal loop.

I don’t know that this is something that is worth putting in the controller. Then I’m arranging the entire grid at once. These views are ‘endless’ in that I’m appending to the list if more files are present when you scroll to the bottom of the page.

Okay, in case anyone else wants to do this, here’s what I came up with


	<% tmp_array = [] %>
	<% counter = 0 %>
	<% collection_count = user_assets.count %>

	<% user_assets.each_with_index do |asset, index| %>
		<%# I have a 12 column grid %>
		<% if counter == 12 %>
			<% counter = 0 %>
		<% end %>

		<% if tmp_array.length > 0 %>
			<% tmp_array.each do |leftover| %>
				<%# if we're at the end of the list, just dump everything to screen %>
				<% if collection_count == index+1 %>
					
					<%= render partial %>
					<% tmp_array.delete(leftover) %>

				<% else %>

					<%# get the icon width value %>
					<% asset_value = asset_layout_column_value(leftover) %>
					<% unless counter + asset_value > 12 %>
						<%= render partial %>
						<% tmp_array.delete(leftover) %>

						<% counter += asset_value %>
					<% end %>

				<% end %>
			<% end %>
		<% end %>

		<% asset_value = asset_layout_column_value(asset) %>
		<% if counter + asset_value > 12 %>
			<% tmp_array << asset %>
		<% else %>
			<%= render partial %>
			<% counter += asset_value %>
		<% end %>
	<% end %>


I trust you that it’s working but I would never ever want to touch that code, that’s a problem for any codebase.
It seems like a fools errand to me as surely your grid would break at different resolutions - I’d just embrace the fluid canvas of the web and let things flow around each other as best as they can.

Agreed it’s ugly…*but as far as breaking in the view, it’s based on the Bootstrap grid system and responds to different resolutions. I think I can ‘pretty’ up the view by putting some things into helpers, but I think the concept is fairly solid. The most awkward thing I found is appended new elements to the list (infinite paging). I had to create a global JS variable to keep track of where the column count is and then append that to url params.

If anyone else has a “it’d be better if…”, I’d love to see it.

I only meant that because it’s a visual problem of layout it might be simpler/better to change the look rather than implementing logic like this in the view.
Here’s one example of how it could be done.