Because of a fire on our Datacenter (for real), we setup a fresh server without any Datas in it, and our user started to use it despite it missing millions of datas.
We may be able to get access to our backup quickly and I'm started to prepare the merging process between the Real and backup Production
Db and the new and temp Production
Db.
For making it simple, let's use this convention:
Real and backup Production
=> production_db
new and temp Production
=> migrate_from_db
We got a User
model with multiple has_one
Association and multiple has_many
Association.
We could set an exemple like this:
class User < ApplicationRecord
devise :database_authenticatable, :recoverable, :rememberable, :validatable, :trackable, :registerable, :omniauthable, omniauth_providers: %i[facebook]
validates :user_name, presence: true, uniqueness: true, format: { with: /\A([\p{L}0-9\_\-]{4,127}\z)/u, message: I18n.t("devise.registrations.new.username_invalid") }, if: :should_validate_user_name?
has_many :tokens
has_many :scenes
has_many :related_users, through: :related_scenes, class_name: "User", source: "user"
has_many :notifications, foreign_key: "target_user_id"
belongs_to :track_color, class_name: "UserColor", optional: true
has_one_attached :profile_picture
end
So I started to make it with my current knowledge with something like:
User.all.each do |user|
ActiveRecord::Base.establish_connection :production
targetedUser = User.where("id = ? OR email = ? OR user_name = ?", user.id, user.email, user.user_name)
if !targetedUser.exists?
targetedUser = User.create user.attributes
end
# How can I after this, insert all has_many associations to targetedUser ?
end
Does exists a way to automatically fetch and insert the associations (has_many
, has_one_attached
, belongs_to
) for the new (or existing) targetedUser ?
May be it could be done with some tools such as Capistrano, but i'm pretty new on Rails.
Thanks a lot by advance for any helps