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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:51:09+00:00 2026-06-12T23:51:09+00:00

So I want to dynamically pass filter parameters to my where method so basically

  • 0

So I want to dynamically pass filter parameters to my where method so basically I have this

@colleges = College.where(@filter).order(@sort_by).paginate(:page => params[:page], :per_page => 20)

And the @where is just a string built with this method

def get_filter_parameters
if params[:action] == 'index'
    table = 'colleges'
    columns = College.column_names
else
    table = 'housings'
    columns = Housing.column_names
end

filters = params.except(:controller, :action, :id, :sort_by, :order, :page, :college_id)
filter_keys = columns & filters.keys

@filter = ""
first = true

if filter_keys
    filter_keys.each do |f|
        if first
            @filter << "#{table}.#{f} = '#{filters[f]}'"
            first = false
        else
            @filter << " AND #{table}.#{f} = '#{filters[f]}'"
        end
    end
else
    @filter = "1=1"
end

The problem is I don’t know how good it is to drop raw SQL into a where method like that. I know normally you can do stuff like :state => ‘PA’, but how do I do that dynamically?

UPDATE

Okay so I am now passing a hash and have this:

if params[:action] == 'index'
    columns = College.column_names
else
    columns = Housing.column_names
end

filters = params.except(:controller, :action, :id, :sort_by, :order, :page, :college_id)
filter_keys = columns & filters.keys

@filter = {}

if filter_keys
    filter_keys.each do |f|
        @filter[f] = filters[f]
    end
end

Will that be enough to protect against injection?

  • 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-12T23:51:10+00:00Added an answer on June 12, 2026 at 11:51 pm

    in this code here:

    College.where(:state => 'PA')
    

    We are actually passing in a hash object. Meaning this is equivalent.

    filter = { :state => 'PA' }
    College.where(filter)
    

    So you can build this hash object instead of a string:

    table = "colleges"
    field = "state"
    value = "PA"
    
    filter = {}
    filter["#{table}.#{field}"] = value
    filter["whatever"] = 'omg'
    
    College.where(filter)
    

    However, BE CAREFUL WITH THIS!

    Depending on where this info is coming from, you be opening yourself up to SQL injection attacks by putting user provided strings into the fields names of your queries. When used properly, Rails will sanitize the values in your query. However, usually the column names are fixed by the application code and dont need to be sanitized. So you may be bypassing a layer of SQL injection protection by doing it this way.

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

Sidebar

Related Questions

I have a list of parameters and I have the method name. I want
I've custom view on account entity and I want to pass parameters dynamically to
I want to dynamically create instance method of child class through class method of
I want to dynamically add links to my pages something like this: foreach (Node
I want to pass the sort field and direction dynamically to the LINQ to
I have a method in a rails helper file like this def table_for(collection, *args)
I want to pass the value to the tag of applet dynamically. I am
I want to pass the movie url from my dynamically generated button to MediaPlayer:
So I want to dynamically have new_course_discussion_path(@discussable) or new_group_discussion_path(@discussable) depending on the @discussable class
I want to pass an array created dynamically in Perl to a JavaScript function.

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.