Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3307498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:24:47+00:00 2026-05-17T21:24:47+00:00

I think what I’m trying to do is pretty simple, and I’m really not

  • 0

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.

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. (

I a model called Attractions, and each attraction has a Category (right now the Category is hardcoded in the DB for simplicity).

This is the Attraction Model and a class method “all_categories” defined…

class Attraction < ActiveRecord::Base

  def self.all_categories
    Attraction.select("DISTINCT category")
  end

end

This is the Attraction Controller

class AttractionsController < ApplicationController
  def index
    @categories = Attraction.all_categories
    @attractions = Attraction.find(:all)
  end

  def show
    @attraction = Attraction.find(params[:id])
  end
end

This is the code in my view that is causing trouble – no rocket science, just a simple iterator, …

  <%= @categories.each do |c| %>
    <%= c.category %><br/>
  <% end %>

Pretty simple, right? This is all running fine, BUT this is what I see when that code segment is run:

Architecture
Art
Fashion
Music
[#<Attraction category: "Architecture">, #<Attraction category: "Art">, #<Attraction category: "Fashion">, #<Attraction category: "Music">]

Why is the array at the end printed? All I want is a list of the categories:

Architecture
Art
Fashion
Music

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?

Appreciate any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:24:48+00:00Added an answer on May 17, 2026 at 9:24 pm
    # Change this line with an =:
    <%= @categories.each do |c| %>
    # ...to this:
    <%  @categories.each do |c| %>
    

    You only want the side effects on the block of the #each method, you don’t want interpolation of the returned value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Think about a table like this ID Value 100 1 100 3 101 1
think this should be an easy one... I want to get the currently selected
I think they are same thing but my boss say that is not right.
I think this should be an easy problem to solve, however its turned into
I think my point is clear, under any condition, could this throw null-reference exception?
I think this property is quite useful, http://www.w3schools.com/Dom/prop_document_xml.asp But as you can see, it's
I think there are people who may be able to answer this, this is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Think of a small and basic affiliate system. I want an URL like www.myshop.com/mynewproduct.html?afid=123
Think that says it all?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.