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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:21:19+00:00 2026-06-10T10:21:19+00:00

In my controller action, I have the following: def index @articles = (params[:mine] ==

  • 0

In my controller action, I have the following:

def index
    @articles = (params[:mine] == "true") ? current_user.articles : Article.search(params[:search])
    @articles = @articles.sort! { |a,b| b.created_at <=> a.created_at }
    @articles = Kaminari.paginate_array(@articles).page(params[:page]).per(25)

    respond_to do |format|
      format.html
      format.json { render json: @articles }
    end
end

And in the model:

  def self.search(search)
    if search.present?
      where("category LIKE ? OR article_type LIKE ?", "%#{search}%","%#{search}%")
    else
      find(:all)
    end
  end

I understand that SQL injection would be possible if you use params directly in a query. Here, I’m passing params directly to the where query through Article.search(params[:search]). Is this prone to SQL injection? If it is, how can I make it more secure? I also have my doubts if I’ve written the controller code properly. If you have suggestions in refactoring the controller code, please do let me know and they’ll be very much appreciated. Thanks a lot!

  • 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-10T10:21:20+00:00Added an answer on June 10, 2026 at 10:21 am

    For your query, you should try to use the methods provided by ActiveRecord or go through Arel itself (your current method is fine though). This will ensure your SQL is properly escaped. If you don’t want to go into details of Arel right now, you can use gems like squeel or meta_where (for older rails).

    I highly recommend these gems for most of your query building needs. Anything more advanced you can write out directly using Arel.

    I can’t recall, if you can do matches (LIKE) directly out in basic ActiveRecord.where syntax, without the help of gem, yet. But you can definitely do this directly in Arel.

    articles = Article.arel_table
    articles = articles[:category].matches("%#{search}%").
      or(articles[:article_type].matches("%#{search}%"))
    

    At this point you can do a to_a on articles or use to_sql and pass this to your Article model using find_by_sql.

    Article.find_by_sql articles.to_sql
    

    will_paginate has a paginate_by_sql method, and I would assume kaminari would have one as well (or at least I would think it would).

    As for your controller code, I would pass any type of sorting option off to the database (this goes for your pagination as well), if you possibly can.

    articles.sort('`articles`.created_at DESC')
    

    The method your using now will grab “ALL” the [allowed] records then sort, then give back your per_page limit. Which kind of defeats the purpose of paginating at all, in this case.

    At a minimum, try to refactor your current setup as:

    @articles = (params[:mine] == "true") ? current_user.articles : Article.search(params[:search])
    @articles = @articles.sort('`articles`.created_at DESC').page(params[:page]).per(25)
    

    As long as your passing around an ActiveRelation you can bind additional stuff to this due to the way Rails lazyloads it’s queries to the database.

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

Sidebar

Related Questions

I have the following in my controller: def index @games = Game.gamsearch(params[:gamsearch]) end def
I have a controller that has the following index action : def index if
I have the following code in my gsp <select onChange=${remoteFunction(action:'superior', controller:'employee', params: '\'name=\' +
Supposing I have the following action: def index @posts = Post.joins(:tags).where(:tags => {:id =>
I have the following controller code: def create @admin = Admin.new(params[:admin]) respond_to do |format|
Okay, I have the following create action #posts_controller, nested resource under discussions def create
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
I have the following route: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with
I have the following route: {language}/{controller}.mvc/{action}/{id} Once a user has choosen the language it

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.