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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:48:50+00:00 2026-06-15T12:48:50+00:00

This code doesnt feel right. I’m trying to make my controllers skinny and I

  • 0

This code doesnt feel right. I’m trying to make my controllers skinny and I feel like there is way too much logic in the controller action.

What would be a much better way of organising this code?

def search
    where_obj = {:status => 1}

    if params[:city].present?
        where_obj.merge! :city => params[:city]
    end

    if params[:county].present?
        where_obj.merge! :county => params[:county]
    end

    ## THERE WILL BE MANY MORE IF STATEMENTS HERE DUE TO GROWING SEARCH FORM

    @person = Person.where(where_obj)
end

As the search form grows this controller action will grow also. How can I keep my controller skinny?

  • 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-15T12:48:51+00:00Added an answer on June 15, 2026 at 12:48 pm

    How about:

    def search
      query = { :status => 1 }.merge(params.select { |k,_| [:city, :country].include?(k) })
      @person = Person.where(query)
    end
    

    Or a somewhat simpler version using the slice method provided by ActiveSupport (in Rails):

    def search
      query = { :status => 1 }.merge(params.slice(:city, :country))
      @person = Person.where(query)
    end
    

    If you’re going to have a bunch of different params to selectively include, then you could group them like this:

    def search
      search_params = [:city, :country, :continent, ...]
      query = { :status => 1 }.merge(params.slice(*search_params))
      @person = Person.where(query)
    end
    

    (Thanks @ajcodez for pointing out the need for a splat on search_params when passed to slice.)

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

Sidebar

Related Questions

How come this code doesnt compile? class A { class B { public enum
i am a beginner and i have a problem : this code doesnt compile
This code used to work but doesnt any more. i used a breakpoint, and
my app crashes with this code.. it doesnt even start up.. any ideas guys
For some reason my code here (this is the entire thing) doesnt actually render
I've been wrestling with this code for a while, trying to clean it up
So I came across some code this morning that looked like this: try {
I'm trying to choose the best way to implement this UI in MVVM manner.
I came across some code that looked like this in a code review the
This code doesn't work propely: I call a function passing a variable What's the

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.