Libor Zahrádka Libor Zahrádka - 1 year ago 66
Ruby Question

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

statement where can i just add new values?

  • I'm pulling DB (JSON) from BitBucket(commits)

  • Then i'm saving that into my Rails DB and returning in view.

  • I tried for that use whenever gem.


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

I have to run first in Rails console
to save into DB.

Thanks for any advice and help.

Answer Source

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)

Doku for 'exists?':

Alternatively you could use 'first_or_create':

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download