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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:24:18+00:00 2026-06-07T16:24:18+00:00

I am trying to create a scope string for a search form. Can I

  • 0

I am trying to create a scope string for a search form. Can I build something like this with scopes:

scopestring = Product.all 
if params[:price].include? 
  scopestring = scopestring + '.free'
if params[:location].include? 
  # same for location 
end 

Obviously I can’t do that, but is there a way to do something similar. Any suggestions? Not a fan of 9 condition IF or case statements to handle various filters on the search page.

If I am going totally in a wrong direction here, what is the best way to handle long conditional statements in Rails? I would prefer staying away from gems.

  • 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-07T16:24:21+00:00Added an answer on June 7, 2026 at 4:24 pm

    First, Product.all isn’t a scope, it actually does the search. What you want is Product.scoped. The scope returned is an object, not a string. Assuming that :free is a scope:

    products = Product.scoped
    if params[:price] == "free"  #your example of include? doesn't make sense
      products = products.free
    end
    

    You can simplify it a bit more by refactoring into the model:

    class Product < ActiveRecord::Base
      scope :free, lambda {|free| free.nil? ? {} : where(:price => 0)}
    end
    

    and then in your controller:

    @products = Product.free(params[:price])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do something like this: for (int i = 1; i
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a macro which can be used for print debug messages when
Trying to create a background-image slideshow and am getting this error... This is the
I'm trying to create a simple scope that sphinx will index (Ruby on Rails).
I am trying to create a simple WF4 activity that accepts a string that
This has been stumping me for a while. I am trying to create a
I'm desperately trying to create a relatively concise search engine with PHP, mySQL and
I'm trying to create an excel spreadsheat in my seam java application. This is
in my project.rb model, I'm trying to create a scope with a dynamic variable:

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.