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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:14:32+00:00 2026-05-19T11:14:32+00:00

In Ruby on rails 3 I want to query on a has_many field of

  • 0

In Ruby on rails 3 I want to query on a has_many field of a model as follows:

@project.items.where(:status => 1)

The problem is I’m trying to get the exact opposite result than this. What i want is all items of @project where the status is not 1. Been looking for the answer to this for a while, anyone?

  • 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-19T11:14:33+00:00Added an answer on May 19, 2026 at 11:14 am

    There are many ways to accomplish what you are trying to do, however, some are better than others. If you will always be searching for a hardcoded number (i.e. 1 in this case), then the following solution will work:

    @project.items.where('status != 1')
    

    However, if this value is not hard-coded, you are openly vulnerable to SQL injection as Rails will not (cannot) escape this kind of query. As a result, it is preferred among Rails developers to user the following syntax for most custom conditions (those that can’t be constructed via Hash):

    @project.items.where(['status != ?', 1])
    

    This syntax is slightly confusing, so let me go over it. Basically you are providing the where clause an Array of values. The first value in the array is a String representing the query you want executed. Anywhere you want a value in that string, you place a ?. This serves as a placeholder. Next, you add an element for every question mark in you query. For example, if I had the following:

    where(['first_name = ? AND last_name = ?', params[:first_name], params[:last_name]]
    

    Rails will automatically match these up forming the query for you. In that process, it also escapes potentially unsafe characters, preventing injection.

    In general, it is preferred to use the Array syntax, even for a hardcoded value. I’ve been told that pure string conditions in Rails 3.5 will raise a warning (unverified), so it doesn’t hurt to get in the process of using the Array syntax now.

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

Sidebar

Related Questions

I am using Ruby on Rails. I want to create a filter field on
I'm trying to get tags working in my rails application and want to use
i want to download ruby on rails, but i am not understanding what to
I want regular expression in ruby on rails which remove all the html tags
I want to log user's actions in my Ruby on Rails application. So far,
it's about Ruby On Rails. I want to use Googleauth and need to add
I am writing a Ruby on Rails app and I want a method to
I am building a Ruby on Rails application and I want to be able
I want to upload and then process a file in a Ruby on Rails
Using Rails 3.1.3 and Ruby 1.9.3. I want to give the user a list

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.