Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby / Rails - .each Iterator is printing entire array at the end of the loop
    primarykey
    data
    text
    <p>I think what I'm trying to do is pretty simple, and I'm really not sure why this isn't working. I'm using Rails 3. </p> <p>Essentially, I'm just trying to select the distinct values from a column in an existing model, and print them out all. For the most part, this works but the .each loop in my view also ends up printing the entire array at the end of the loop. (</p> <p>I a model called Attractions, and each attraction has a Category (right now the Category is hardcoded in the DB for simplicity). </p> <p>This is the Attraction Model and a class method "all_categories" defined...</p> <pre><code>class Attraction &lt; ActiveRecord::Base def self.all_categories Attraction.select("DISTINCT category") end end </code></pre> <p>This is the Attraction Controller</p> <pre><code>class AttractionsController &lt; ApplicationController def index @categories = Attraction.all_categories @attractions = Attraction.find(:all) end def show @attraction = Attraction.find(params[:id]) end end </code></pre> <p>This is the code in my view that is causing trouble - no rocket science, just a simple iterator, ...</p> <pre><code> &lt;%= @categories.each do |c| %&gt; &lt;%= c.category %&gt;&lt;br/&gt; &lt;% end %&gt; </code></pre> <p>Pretty simple, right? This is all running fine, BUT this is what I see when that code segment is run:</p> <pre><code>Architecture Art Fashion Music [#&lt;Attraction category: "Architecture"&gt;, #&lt;Attraction category: "Art"&gt;, #&lt;Attraction category: "Fashion"&gt;, #&lt;Attraction category: "Music"&gt;] </code></pre> <p>Why is the array at the end printed? All I want is a list of the categories:</p> <pre><code>Architecture Art Fashion Music </code></pre> <p>Obviously, I'm new to Ruby/Rails, and I've tried to search all over for a solution to this. Is there something obvious that I'm missing?</p> <p>Appreciate any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload