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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:32:58+00:00 2026-06-04T15:32:58+00:00

I have two separate controllers that inherit from Admin::UserBaseController , display a searchable, sortable

  • 0

I have two separate controllers that inherit from Admin::UserBaseController, display a searchable, sortable table of users, and use the same partial views.

  • Admin::UsersController – Display users within the context of a given organization.
  • Admin::OrganizationsController – Displays all users for the system.

Here is the index method of Admin::UsersController:

  def index
    q = "%#{params[:search]}%"
    @users = User.where("first_name like ? or last_name like ? or username like ?", q, q, q).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page])

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

Here is the edit method of Admin::OrganizationsController:

def edit
    @organization = Organization.find(params[:id])
    q = "%#{params[:search]}%"
    @users = @organization.users.where("first_name like ? or last_name like ? or username like ?", q, q, q).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page])
end

There is a lot of similarity between the two methods in the way that the @users variable is assigned. It’s a difference of User and @organization.users and that’s it. How do I DRY this up?

  • 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-04T15:32:59+00:00Added an answer on June 4, 2026 at 3:32 pm

    So what this screams is scopes. This removes the duplicate queries into a single place in the model and enables you to chain scopes onto the class and associations.

    class User < ActiveRecord::Base
      scope :search_identity, lambda { |identity| where("first_name like ? or last_name like ? or username like ?", identity, identity, identity) }
      scope :user_order, lambda { |column,direction| order("#{column} #{direction}") }
    end
    

    Then in Admin::UsersController

    q = "%#{params[:search]}%"
    @users = User.search_identity( q ).user_order( sort_column, sort_direction).paginate(:page => params[:page])
    

    In Admin::OrganizationsController:

    q = "%#{params[:search]}%"
    @users = @organization.users.search_identity( q ).user_order( sort_column, sort_direction).paginate(:page => params[:page])
    

    Making everything nice and succinct.

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

Sidebar

Related Questions

I have two separate applications (both part of the same system) that share a
I have two separate sets of tables in the same database that model the
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)
I have two separate caches running in a JVM (one controlled by a third
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
We have two databases, in two separate locations. One of the databases resides in
Say I have two separate classes, A and B. I also have Repository class
Is it possible to combine Underscore's filter and map? I currently have two separate
I have two essentially separate applications for configuring two pieces of hardware sold by
I have two physically-separate MySQL databases on which I have to run a single

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.