The query you're looking for would be like:
Profile.where("name LIKE ?", "%#{profiles.name}%")
But note that your @related_profiles may not be properly assigned as the result would be the same as saying:
Profile.where("name LIKE ?", "%#{@favorites.last.name}%")
whereas, I doubt if that is what you need.
Also note that the it would be an ActiveRecord::Collection, an array like object.
A way to work around that is to initialize @related_profiles = [] and then at each point through your loop, you could do:
@related_profiles +=
Profile.where("name LIKE ?", "%#{profiles.name}%")
or another way is:
names = @favorites.map(&:name)
query = names.map{|name| "name LIKE '%#{name}%'"}.join(" OR ")
OR
query = @favorites.map{|favorite| "name LIKE '%#{favorite.name}%'" }.join(" OR ")
THEN
profiles = Profile.where(query)
UPDATE
Based on a comment from @joshrumbut, I decided to reimplement using the bind parameters.
However, code clarity is a bit lost, but here's a way it could be done:
names = @favorites.map(&:name)
query = names.map{|favorite| "name LIKE ?" }.join(" OR ")
profiles = Profile.where(query, *names.map{|name| ("%#{name}%")})
Based on the comment from @muistooshort, I removed the quotes from the first two queries and I think this approach looks a bit cleaner, as he suggested. From the docs
Profile.where('name like any(array[?])', names.map { |s| "%#{s}%" })