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 8614577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:09:12+00:00 2026-06-12T05:09:12+00:00

I have an application where I have added a sidebar with a list of

  • 0

I have an application where I have added a sidebar with a list of all the categories the products belong to.

The feature works fine but there is a problem.

The code only scans through the products on the current page and not the actual categories available. That is:

In my application there are say 5 categories (which there are) of products but on the current page there are only 3 products. So the code only shows me 3 categories and not the actual 5.
How can I fix this? Does this have anything to do with instance variables?

Here is the code from application.html.erb

<div>
  <% a = [""] %>
    <h1>Categories</h1>
    <% @products.each do |product| %>
    <% a = a + [product.category] %>
    <% a = a.uniq %>        
  <% end %>

  <% a.each do |c| %>
     <p class="text-error"><%= c %></p>
  <% end %>
</div>

Migration files..

class CreateProducts < ActiveRecord::Migration
   def change
     create_table :products do |t|
       t.string :name
       t.text :description
       t.date :delivery_date
       t.decimal :price

       t.timestamps
    end
   end
 end

 class CreateCommentsTable < ActiveRecord::Migration
  def up
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :product

      t.timestamps
    end
    add_index :comments, :product_id
  end

  def down
  end
 end


 class AddCategoryToProducts < ActiveRecord::Migration
   def change
     add_column :products, :category, :string
   end
end

Routes..

resources :products do
    resources :comments 
end

The code is available at https://github.com/abhishekdagarit/sample-app.git

Might take u people just a quick look to answer this…

  • 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-06-12T05:09:13+00:00Added an answer on June 12, 2026 at 5:09 am

    If you have separate Category model mapped to categories table then Category.select(:name).uniq would have return unique categories.

    In your case you have category column withing the table. So in that case

    Product.select(:category).uniq would return list of Products with unique categories.

    So now you template would be cleaner with:

    <% for product in Product.select(:category).uniq %>
       <%= product.category %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have QGraphicsScene with pixmap added and all is viewed in
I have added some settings to my c# application using the configuration editor. There
I have an application where each screen will be added dynamically, along with all
In my application i have added flurry analytic but even after days of adding
I have an application with a database that I am running. I added all
In a C# Winforms (3.5) application I have added a class that contains many
I have a serial application that I parallelized using OpenMP. I simply added the
I have added options in application.rb: config.autoload_paths += %W(#{config.root}/lib) config.autoload_paths += Dir[#{config.root}/lib/**/] and lib\functions.rb:
I have just added a Core Plot view to my application based on a
I have just added a Telerik menu to my MVC application. I also have

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.