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

  • Home
  • SEARCH
  • 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 7812531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:32:21+00:00 2026-06-02T04:32:21+00:00

I am trying to write active record queries in my model for an advanced

  • 0

I am trying to write active record queries in my model for an advanced search.

I have a Firms model and each firm has many Categories through Categorizations

I have the following built into my search method at the moment.

def find_firms
    firms = Firm.order(:name)
    firms = firms.where("name like ?", "%#{name}%") if name.present?
    firms = firms.categories.find(id: category_id) if category_id.present?
end

My category schema looks like this.

create_table "categories", :force => true do |t|
  t.text     "name"
  t.datetime "created_at", :null => false
  t.datetime "updated_at", :null => false
 end

and my categorizations

 create_table "categorizations", :force => true do |t|
   t.integer  "category_id"
   t.integer  "firm_id"
   t.datetime "created_at",  :null => false
   t.datetime "updated_at",  :null => false
 end

and my search form view

  <div class="field">
    <%= f.label :category_id, "Practice Area" %><br />
    <%= f.collection_select :category_id, Category.order(:name), :id, :name, include_blank: true %>
  </div>

Any ideas what I am doing wrong?

As i keep getting the error:

 undefined method `categories'

Many thanks.

Ross

  • 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-02T04:32:23+00:00Added an answer on June 2, 2026 at 4:32 am

    You are trying to run the categories method on the array returned by firms which will fail. I am assuming you want to return the firms with the given category id.

    def find_firms
      firms = Firm.order(:name)
      firms = firms.joins(:categories).
                where(:categories =>{ id : category_id}).uniq if category_id.present?
      firms = firms.where("name like ?", "%#{name}%") if name.present?
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an ActiveAdmin form for my Gallery model that has a
I'm trying to write a registration using devise and active merchant. The form is
I am trying to write a function completed_steps_for_course(course) in my User model that finds
I have a problem trying to update an attribute of one model using the
I'm trying to figure out how to save an active record instance to the
I'm new to rails and trying to write my first app. I have a
I am trying to set up a Many-to-Many association between 2 objects. I have
I'm trying to write a route that captures the one-to-many relationship between posts and
I am trying to write a unit testing for a User model in Ruby
I am trying to write a forum with Ruby on Rails. On model side,

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.