简体   繁体   中英

ROR…Retrieve data from db

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM