I am trying to implement a search feature to go across multiple tables. Right now I only have searching functionality for each of the individual tables in the database.
As an example I would like to be able to search the 'title' from multiple tables that all correspond to that title. The two tables in this example are 'acts' and 'attachments'.
Right now I have:
acts:
Controller
def index
if params[:search]
@acts = Act.search(params[:search]).order('title DESC')
else
@acts = Act.all.order('title DESC')
end
end
Model
class Act < ActiveRecord::Base
def self.search(query)
where("title like ?", "%#{query}%")
end
end
attachments:
Model:
class Attachment < ActiveRecord::Base
def self.search(query)
where("title like ?", "%#{query}%")
end
end
Controller:
def index
if params[:search]
@attachments = Attachment.search(params[:search]).order('title DESC')
else
@attachments = Attachment.all.order('title DESC')
end
end
Any help would be greatly appreciated. I am new to RoR and am trying to implement this the correct way.
Thank you.