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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:50:56+00:00 2026-06-12T17:50:56+00:00

I have the following controller that retrieves subcategories, and then loops through each subcategory,

  • 0

I have the following controller that retrieves “subcategories”, and then loops through each subcategory, grabbing and storing what I’ve termed “circles” into an instance variable

  def show
    @sub_categories = Category.where(:parent => params[:id])
    @sub_categories.each do |sub_cat|
      i = 1
      @circles[i] = Circle.where(:category_id => sub_cat.id)    
      i++
    end
  end

This is outputting an error. I’m coming from PHP and don’t know how to do this properly.

categories_controller.rb:9: syntax error, unexpected kEND
  • 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-12T17:50:58+00:00Added an answer on June 12, 2026 at 5:50 pm

    IMO you should just use a join:

    @circles = Circle.joins(:categories).where(:categories => { :parent_id => params[:id] })
    

    To address the code you’ve written: adding to an array in Ruby is done with <<. So you could rewrite your code as follows:

    @circles = []
    @sub_categories.each do |sub_cat|
      sub_cat.circles.each do |circle| # assumes Category has_many :circles
        @circles << circle
      end
    end
    

    But you’d better use joins to do it in one query, like I show above.

    Update based on pastebin:

    There is no need to set @circles.

    This is what your view should look like:

    <% @sub_categories.each do |sub_cat| %>
      <div class="circle">
        <header>
          <h3 data-toggle="collapse" href="#<%= dom_id(sub_cat) %>-collapse"><%=sub_cat.name %></h3>
          <ul id="<%= dom_id(sub_cat) %>-collapse" class="accordion-body collapse">
            <li class="list-shadow"></li>
    
            <% sub_cat.circles.each do |circle| %>
              <li><%= link_to circle.title, circle, :remote => true, :method => 'get' %></li>
            <% end %>
    
          </ul>
        </header>
      </div><!--.circle-->
    <% end %>
    

    To prevent the N+1 queries problem, you have to do this in your controller:

    @sub_categories = Category.includes(:circles).where(:parent => params[:id])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following getJSON that calls an MVC controller which returns inforamtion back:
If i have a controller action Create that returns a view with the following
I have the following method that retrieves all the appointments between 2 dates: def
I have the following controller that returns a list of tags when it receives
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
I have the following controller: class Tests extends CI_Controller { public function update_record_test() {
I have the following controller: public ActionResult Search(string Name, int? Friend, int? Page) It
I currently have the following controller method in a Rails app: def index @entries
I have the following view controller in my UIPopover: @protocol StorePopoverDelegate - (void)storeSelected:(NSString *)store;
I have following spec to test controller method: context #create do it should redirect

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.