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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:25:54+00:00 2026-05-14T21:25:54+00:00

Rails and MySQL: I have a table with several boolean columns representing tags. I

  • 0

Rails and MySQL:

I have a table with several boolean columns representing tags. I want to find all the rows for which a specific one of these columns is ‘true’ (or I guess in the case of MySQL, ‘1’). I have the following code in my view.

@tag = params[:tag]

@supplies = Supply.find(:all,
  :conditions=>["? IS NOT NULL and ? !=''", @tag, @tag], :order=>'name')

The @tag is being passed in from the url. Why is it then that I am instead getting all of my @supplies (i.e. every row) rather than just those that are true for the column for @tag.

Thanks!

  • 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-14T21:25:55+00:00Added an answer on May 14, 2026 at 9:25 pm

    If params[:tag] is set to foo, the find method is generating this query:

    select * from supplies where 'foo' is not null and 'foo' != '' order by name;
    

    This is returning all your Supply records because both conditions are always true.

    1. 'foo' is not null
    2. 'foo' != ''

    Of course you’re intending for params[:tag] to be a column name, but that is just terrible design.

    You should have a tag attribute on your Supply model and use the following finder:

    @supplies = Supply.all(:conditions => ["tag = ?", params[:tag]], :order => "name")
    

    If you really want the ability for Supplies to have the option for multiple Tags, use:

    class Supply < ActiveRecord::Base
      has_and_belongs_to_many :tags
    end
    
    class Tag < ActiveRecord::Base
      has_and_belongs_to_many :supplies
    end
    
    @supplies = Supplies.all(:conditions => {:tags => ['foo', 'bar']}, :order => "name")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Environment: Rails 3.0.1, MySQL I have a Users table, and I want to search
Say that I have a table with a bunch of records, which I want
I have a table in a Mysql db that I want to use multicolumn
I have the below table ID First Name Last Name 1 Ruby Rails 2
I have the following Rails migration which works perfectly (irrelevant pieces removed): create_table :comments
We have a Rails application that is running in a MySQL master-slave set-up for
I'm developing a rails application with MYSQL and I've created a migration for adding
I have a legacy table with (horrible) column names like LYEAR#2, LYEAR#3 ... LYEAR#9
Suppose I have a database containing 3 tables in a grails application: User Activity
I need some help figuring out the best way to proceed with creating a

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.