Reputation: 21
For every original_url
(entered by user) I generate a short_url
, and, after saving them into the db, show the short_url
in place of the original_url
. I was able to do up to this.
Now I want to see that if an URL has already been shortened, so it can be retrieved from the database directly without shorting again.
My idea of doing this is to compare original_url
entered by a user to the ones in db, and if found retrieve short_url
directly, else shorten original_url
.
But I am unable to do this ... please help me. If there is any other better idea please let me know. Thanks in advance.
def show
@url = Url.find(params[:id])
if Url.find_by_original_url(@url.original_url)
@url.short_url
else
@short_url
end
end
Upvotes: 0
Views: 92
Reputation: 31040
If you're filling the short_url column then why not just check to see if there's something in there?
def show
@url = Url.find(params[:id])
unless @url.short_url
# shorten the URL
end
end
or
def show
@url = Url.find(params[:id])
if @url.short_url
else
# shorten the URL
end
end
Upvotes: 3