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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:22:39+00:00 2026-06-08T05:22:39+00:00

Basically in my application a song has many categories and a category has many

  • 0

Basically in my application a song has many categories and a category has many songs, through a joining model called categorizations.

I basically have a search function in the song model which looks like this at the moment:

def self.search(search)
    if search
      find(:all, conditions: ['title LIKE ?', "%#{search}%"], order: 'title')
      find(:all, conditions: ['lyrics LIKE ?', "%#{search}%"], order: 'title')
    else
      #if nothing is inputed then just show all songs
      find(:all, order: 'title')
    end
  end

And that works fine as title and lyrics are part of the song model. However I’m not sure how I’d go about adding a function so that you can input the category description (the only attribute that the category model has) and have it return that in the search results as well, given that it’s in a separate model. Thanks in advance for any help!

  • 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-08T05:22:40+00:00Added an answer on June 8, 2026 at 5:22 am

    Use ActiveRecord includes method.

     class Song < ActiveRecord::Base
       has_many :categorizations
       has_many :categories, :through=>:categorizations
    
       def self.search(search)
        res=includes(:categories).order('title')
        res=res.where('title LIKE :search OR lyrics LIKE :search',:search=>"%#{search}%") if search
        res
      end
    
      def self.search_by_category_desc(search)
        res=joins(:categories).order('title')
        res=res.where('categories.description LIKE ?',"%#{search}%") if search
        res
      end
    
      def self.search_by_title_or_lirics_or_cat_desc(search)
        res=includes(:categories).order('title')
        res=res.where('title LIKE :search OR categories.description LIKE :search OR lyrics LIKE :search',:search=>"%#{search}%") if search
        res
      end
     end
    

    This will preload all categories for searched songs.

    Remember about difference between joins and includes: What's the difference between "includes" and "joins" in ActiveRecord query?

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

Sidebar

Related Questions

Basically I have this application which scans through all mp3's in a folder and
I have an iPhone application which basically is getting information from an API (in
I have a simple application which basically consists of a line of buttons and
Basically multiple instances of our application will be launched but they need to have
Android Application : Basically application has to register few credentials like username , password
We currently have a Live ASP.NET application (Basically a CMS) running on our IIS7
I have a list of settings for my application (basically setting which disabled access
I'm making a custom GUI for my application. Basically my application has multiple 'tabs'.
I have a WPF app with a listview control. The application basically loads a
I have a form on my Java application which basically is to provide the

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.