Hello,
I am developing a small user application in Rails 3 and is stuck on a search feature. I got a table called Profile which holds Firstname and Lastname. When searching this table I use:
Profile.find_all_by_firstname(params[:keyword])
The thing is that I want the user to be able to search a table that holds contacts (profile_id and friend_id only) but I want them to be able to seach by name here too. How is this done?
For example:
John searches for the name Ann which is a contact of his. Since the Contact table does not store names the search must include the Profile table in the query. How can I do this?
UPDATE
This join query fetches everyone not only the contacts, can anyone spot a problem with it?
Profile.find_all_by_firstname(params[:keyword],
:select => "firstname, lastname, user_id",
:joins => "left outer join contacts on profiles.id = contacts.profile_id")