I have an array containing URL paths, I want to be able to search in my database in a table which one of the fields (labelled “URL”) for the URLs specified in the array.
So I have a simplified version of what I want to achieve.
urls = ["url1", "url2", "url3", "url4", ... ]
urlsInDB = []
results = Link.all
urls.each() do |url|
if not results.where(:url=>url).blank?
urlsInDB << url
end
end
However I know that you cannot call “.where” on a set of results obtained by “.all”. I want to do it this way so that the database is only ever queried once not for each URL in urls as this can have length n and that would require n queries.
Any ideas?
In SQL, you’d do this by saying:
You can do this with ActiveRecord by saying:
If you just want to get an array of URLs that matched, say: