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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:32:55+00:00 2026-06-12T01:32:55+00:00

I’m a bit lost on how to handle multiple param inputs in a URL

  • 0

I’m a bit lost on how to handle multiple param inputs in a URL to build filters in Rails 3.2

Basically, I would like to take http://example.com/products/?category=24 and http://example.com/products/?category=24&country=24 to work together.

I have the following models Product, Country, Category, Categorization

I can build a simple condition in my ProductController

def index
  @products = product.all

  if params[:country]
    @products = Country.find(params[:country]).products
  end

  if params[:category]
    @products = Category.find(params[:category]).products
  end

end

I wanted to know the best way to tackle this since my Category and Product models through the Categorization model through with a has_many association.

class Product < ActiveRecord::Base

  has_many :categorization
  belongs_to :country

end

class Category < ActiveRecord::Base
  has_many :categorization
  has_many :products, through: :categorization

  attr_accessible :name

end

class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :product

  attr_accessible :category, :product
end

My view layouts template is a simple list of category and country links. So if somebody clicks a category called “Toys” and the clicks on the country “England” it should build a link like so: http://www.example.com/products/category=12&country_id=1

<div class="wrapper">
  <div class="products">
  </div>
  <div class="sidebar>
   <h2>Categories</h2>
   <ul>
     <% @categories.each do |category| %>
       <li><%= link_to category.name, params.merge(category: category.id) %></li>
     <% end %>
   </ul>
    <h2>Countries</h2>
   <ul>
     <% @countries.each do |country| %>
       <li><%= link_to country.name, params.merge(country: country.id) %></li>
     <% end %>
   </ul>
  </div>
</div>

Updated

I’ve gone with the suggestion of creating a filter on the model class and the below code works but it seems quite messy and not DRY.. can someone assist on another approach.

# Product Model

class Product < ActiveRecord::Base 

  # Search filter
  def self.filter_by_params(params)
    if params.has_key?(:category) && params.has_key?(:country_id)
      scoped.joins(:categorization).where("category_id = ? AND country_id = ?", params[:category], params[:country_id])
    elsif params.has_key?(:category)
      scoped.joins(:categorization).where("category_id = ?", params[:category])
    elsif params.has_key?(:country_id)
      scoped.where(country_id: params[:country_id])
    else
      scoped
    end
  end

end

# Product Controller

def index
  @product = Product.filter_by_params(params)
end

Many thanks for any advice.

WD

  • 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-12T01:32:56+00:00Added an answer on June 12, 2026 at 1:32 am

    I would change your ProductsController as follows:

    def index
      @products = Product.filter_by_params(params)
    end
    

    Then in your Product model:

    def self.filter_by_params(params)
      scoped = self.scoped
      scoped = scoped.where(:country_id => params[:country_id]) if params[:country_id]          
      scoped = scoped.where(:category_id => params[:category_id]) if params[:category_id]
      scoped
    end
    

    You’ll need to change your view to pass category_id and country_id.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
We're building an app, our first using Rails 3, and we're having to build
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.