liborza
liborza

Reputation: 1009

Rails updating DB constantly (external values)

I would like to ask if there is any way how to update database from external source to my Rails DB constantly (every 1 hour)...

I was trying to do that but when i do that my DB is duplicated + added new files so is there any if statement where can i just add new values?

bitbucket.rb

class Bitbucket < ActiveRecord::Base
  def self.savedata
    require 'bitbucket_rest_api'   
    bitbucket = BitBucket.new login:'...', password:'...'
    repo = bitbucket.repos.commits.list '...', '...'
    repo["values"].each do |r|
      create(
        name: r["author"]["user"]["display_name"],
        message: r["message"],
        date: r["date"]
      )
    end
  end
end

I have to run first in Rails console Bitbucket.connection then Bitbucket.savedata to save into DB.

Thanks for any advice and help.

Upvotes: 0

Views: 58

Answers (1)

trueunlessfalse
trueunlessfalse

Reputation: 1233

So, If I understand it right you want to pull data from bitbucket every 1 hour, and update your database only with new 'values'. In that case the question how to know if you have an entry in the database already or not is up to you.

For instance you could ask:

author_name = r["author"]["user"]["display_name"]
message = r["message"]
date = r["date"]

unless exists?(name: author_name, message: message, date: date)
  create(name: author_name, message: message, data: date)
end

Doku for 'exists?': http://apidock.com/rails/ActiveRecord/Base/exists%3F/class

Alternatively you could use 'first_or_create':

http://apidock.com/rails/v3.2.1/ActiveRecord/Relation/first_or_create

Upvotes: 1

Related Questions