1

I'm listing a set of data (for simplicity sake, just the identity column) of a particular database table table as follows:

<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
    <%= field.id %>
    <br/>
<% end %>

As you may have gathered from above, I'm using the combination of select and each to iterate ONLY through rows whose column model contains the string PreferredOffering.

My expectation was that I would see a nice ordered list of numbers and indeed I do. My confusion is that I ALSO see the entire @fields array coughed all over the page, below the list of numbers. (See below html excerpt)

106
<br/>

107
<br/>

108
<br/>

109
<br/>

110
<br/>

111
<br/>

112
<br/>
[#&lt;PreferredOfferingField id: 5, field_heading: &quot;Anti-dilution provisions- Typical Weighted Average&quot;, category: &quot;Anti-Dilution&quot;, intra_cat_order: 1, model: &quot;P

My guess is that I'm doing something funny with select as I'm not really familiar with its usage.

Any ideas on how to remedy this would be received gratefully; thanks in advance.

neanderslob
  • 2,633
  • 6
  • 40
  • 82
  • `<%= @fields.select....` to `<% @fields.select ...` – Nithin Jun 04 '15 at 02:16
  • @Nithin Ah crap, you're right; here I thought I was going to learn something. Please feel free to write your comment up as an answer so I can give you credit for proofreading my code. :-P Many thanks! – neanderslob Jun 04 '15 at 02:27

1 Answers1

2

<% %> Executes the Ruby code inside

<%= %> Prints the results

You are displaying array and then it's values, so you would want to change <%= %> to <% %> .

<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>

to

<% @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>
Nithin
  • 3,679
  • 3
  • 30
  • 55