How to avoid duplicate data in my database in ruby on rails

Viewed 1139

I have scrap a data from another website and saved it in my database which is working fine. However, anytime I refresh my application the scrapped data duplicate itself in my database.Any help would be highly appreciated. Below are my code

require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open("www.example.com"))
entries = doc.css('.block')
@entriesArray = []
entries.each do |row|
    Scrap.create!(                  
    title: title = row.css('h2>a').text,
    link:  link = row.css('a')[0]['href'],
    day:   days =row.css('time').text)
    @entriesArray << Entry.new(title,link,days)
end
3 Answers
Related