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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:13:14+00:00 2026-05-18T08:13:14+00:00

How is the best way to load Category model, for ProductController in new, edit

  • 0

How is the best way to load Category model, for ProductController in new, edit update and create actions

Product has categories collection

class Product < ActiveRecord::Base
  has_many :categories
end 

Always for new, edit, create and update actions, I need to load the categories collection to populate a check_box_tag list

The “baby steps” for this situation is:

class Admin::ProductsController < Admin::AdminApplicationController

 def new
   @product = Product.new
   @categories = Category.all
 end

 def edit
   @product = Product.find(params[:id])
   @categories = Category.all
 end

 def create
   @product = Product.new(params[:product])
   if @product.save
     flash[:notice] = 'Product was successfully created'
     redirect_to edit_admin_product_path(@product)
   else
     @categories = Category.all
     render :new
  end
 end

 def update
   @product = Product.find(params[:id])
   @product.update_attributes(params[:product])
   if @product.save
     flash[:notice] = 'Product was successfully updated'
     redirect_to edit_admin_product_path(@product)
   else
     @categories = Category.all
     render :edit
   end
 end
end 

I don’t want to load always Category.all in different situations for same purpose

Options:

First – load categories over the view:

<% Category.all.each do |cat| %>
  <li>
    <%= check_box_tag .... %>
  </li>
<% end %>

:/

Second – load categories over the ProductsHelper :

module ProductsHelper

  def categories
   Category.all
  end

end

:/

“Third” – Exist a filter like ‘before_render’?

class Admin::ProductsController < Admin::AdminApplicationController

  before_render :load_categories :edit, :new

  def load_categories
    @categories = Category.all
  end

end

😀 😀 😀

What’s the rails way for this situations?

Best Regards,
Pablo Cantero

  • 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-18T08:13:14+00:00Added an answer on May 18, 2026 at 8:13 am

    In your controller, or if needed elsewhere, in application_controller.rb:

    def all_categories
      @all_categories ||= Category.all
    end
    helper_method :all_categories
    

    The first time it’s called, it will hit the db, and later it will return the controller instance variable.

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

Sidebar

Related Questions

I'm trying to figure out the best way to load a textured 3D model
Working with the OpenSSL library in c, what is the best way to load
I'm wondering what the best way is to use the XDocument.load and save methods
the best way to explain is with example so: this is the model public
What is the best way to maintain upgrade scripts between versions of a product?
what is the best way to load a xml file as an parsed object
What is the best way to load content into my infowindow using ajax? Right
I have been wondering which is the best way to load a navigation view.
Does anyone know if there is a way to run a product collection through
I was wondering the best way to load a big array which stores map

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.