stadtratmonitor/app/models/paper.rb
Andreas Haller 5ea5aae4b8 Don’t import the same URI multiple times
Use url as unique identifier during import
2015-04-28 00:47:57 +02:00

24 lines
663 B
Ruby

require 'elasticsearch/model'
require 'json'
class Paper < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
class << self
def import_from_json(json_string)
JSON.parse(json_string).each do |record|
attributes = {
name: record['name'],
url: record['url'],
reference: record['reference'],
paper_type: record['paper_type'],
originator: record['originator'],
published_at: record['published_at'],
}
record = find_or_initialize_by(url: attributes[:url])
record.update_attributes!(attributes)
end
end
end
end