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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:39:57+00:00 2026-05-20T01:39:57+00:00

I am fairly new to Ruby on Rails and as a C# developer, when

  • 0

I am fairly new to Ruby on Rails and as a C# developer, when I want to re-use code (for a repository class), I could put it into a base class of type <T> to be able to do something like this:

public virtual IEnumerable<T> GetAll()
{
    return Context<T>.GetAll();
}

If I need to do any custom logic, I could, of course, override the method in my ‘User’ repository.

In Ruby, I am familiar that you can do this:

class UsersController < ApplicationController

This will allow access to all methods in ApplicationController and it’s parent classes. When using scaffolding, it generates the following method in each of my child classes:

def index
  @users = User.all

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @users }
  end
end

What I end up with is 10 classes that have the same method, but the only difference is ‘User.all’, ‘Post.all’, etc.

How would I make this method generic so I can put it in my ApplicationController class?

Thanks for any assistance you can provide to a Ruby on Rails newbie.

  • 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-20T01:39:58+00:00Added an answer on May 20, 2026 at 1:39 am

    The first thing to realize about the scaffolding code is that it can be abreviated, as such:

    def index
      @users = User.all
    end
    

    unless you intend to deliver the view in another format, like json, html, pdf, the respond_to block is unnecessary. If you still feel the need to dry up this method, you could do something like

    # app/controllers/concerns/autoload_records.rb
    
    module AutoloadRecords
      included do
        before_action :load_records, only: :index
        before_action :load_record, only: [:create, :show, :edit, :update, :destroy]
      end
    
      private
      def load_records
        @records = model_class.all
      end
    
      def load_record
        @record = model_class.find(params[:id])
      end
    
      def model_class
        klass = self.class.to_s[/\A(\w+)sController\Z/,1] #=> get the name of the class from the controller Constant
        Object.const_get(klass)
      end
    end
    

    and write your controller like

    class UsersController < ApplicationController
      include AutoloadRecords
    
      def index
        @records # => #<ActiveRecord::Relation[...]>
      end
    
      def show
        @record # => #<User ...>
      end
    
      def non_rest_action
        @record # => nil
        @records # => nil
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new in the Ruby + Rails scene. Although I have a very
I'm fairly new to Ruby on Rails, and I have a project with a
I am fairly new to Ruby on Rails, and I clearly have an active
I'm fairly new to Ruby on Rails, and I'm attempting to create some fancy
I am fairly new to Ruby On Rails and right now I am doing
I'm fairly new to ruby on rails and I'm trying to implment a drag
I am fairly new to Rails, but I am pretty experienced with Ruby. Can
Being fairly new to JavaScript, I'm unable to discern when to use each of
im fairly new to the Google Maps V3 API and i'm running into an
I am using cucumber and RSpec for my new ruby on rails application and

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.