简体   繁体   中英

What is the best way to keep temporary version of database while it is being updated in Ruby on Rails?

I currently have a model in ruby on rails which stores information, but I want to update this information every hour. What's the best way to keep a temporary version of the data while my database is being updated?

I have tried using

@temp = Lecture.deep_dup

But this gets cleared when I delete all from my database.

What's the best approach?

Edit : Here's the code that does the updating

lecture = Lecture.create(name:course_name,section:section,term:term_name,days:course_days,s_time:start_time,e_time:end_time,location:location,also_reg:also_reg,status:status)
lecture.save

This runs for a few minutes while it scrapes a website

Assuming you have some task that mucks with all the data, just wrap that code inside a transaction.

UPDATE based on your update

ActiveRecord::Base.transaction do
  lecture = Lecture.create(
    name: course_name, section: section, term: term_name, days: course_days,
    s_time: start_time, e_time: end_time, location: location, also_reg: also_reg, status: status)
  lecture.save
end

UPDATE 2 per comment, In this case it would be better to do:

Lecture.transaction do
  lecture = Lecture.create(
    name: course_name, section: section, term: term_name, days: course_days,
    s_time: start_time, e_time: end_time, location: location, also_reg: also_reg, status: status)
    lecture.save
  end
end

See https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

But if your Lecture.create method is long running, I would suggest you move it to a worker / background job. See https://guides.rubyonrails.org/active_job_basics.html

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