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!
Use ActiveRecord includes method.
This will preload all categories for searched songs.
Remember about difference between
joinsandincludes: What's the difference between "includes" and "joins" in ActiveRecord query?