2014-06-06 104 views
0

我正在使用rails 4,並希望集成一些外部API,爲此我有一個情況,我必須創建用戶在兩個數據庫同時具有相同的記錄創建設計用戶在兩個數據庫同時記錄相同的兩個數據庫

我使用設計這個Rails應用程序,到目前爲止,我還做了以下保存用戶記錄在另一個數據庫:

config/database.yml

development: 
    adapter: mysql2 
    encoding: utf8 
    database: test_openmeetings_development 
    pool: 5 
    username: root 
    password: root 
    socket: /var/run/mysqld/mysqld.sock 

openmeetings_dev: 
    adapter: mysql2 
    encoding: utf8 
    database: openmeetings 
    pool: 5 
    username: root 
    password: root 
    socket: /var/run/mysqld/mysqld.sock 

models/User.rb

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable, :confirmable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :new_password, :firstname, :lastname, :username, :email, :password, :password_confirmation, :remember_me, :confirmation_token 
    attr_accessor :new_password 

    # creating user record to another database just after Devise user is created 
    after_create :save_to_openmeetings 


    def save_to_openmeetings  
    user = last_user 

    ActiveRecord::Base.establish_connection("openmeetings_dev").connection 
    sql = "INSERT INTO users (firstname, lastname, login) VALUES ('#{user.firstname}', '#{user.lastname}', '#{user.email}')" 
    connection.execute(sql) 

    # reconnect database to original state 
    ActiveRecord::Base.establish_connection("#{Rails.env}").connection 
    end 

    def last_user 
    User.last 
    end 
end 

PROBLE M 1.雖然它正在兩個數據庫(從控制檯日誌)中正確創建用戶,但我面臨的問題是,Data存在另一個數據庫(openmeetings)中,但該數據不存在在我的原始數據庫(test_openmeetings_development)中

  1. 連接還沒有重新回到原來的數據庫(test_openmeetings_development)

可能是什麼我做的是不是一個好主意,所以,請分享我能解決我的問題的邏輯。 (?爲什麼它不工作),或請讓我知道什麼,我在此代碼是缺少

感謝

+0

這似乎是對我來說完全是錯誤的做法。您應該考慮1.主從複製或2.定期快照從一個移動到另一個。你需要兩個數據庫的原因是什麼? –

+0

謝謝黑暗獵鷹給你的建議。還有什麼建議? – przbadu

+0

@przbadu你是如何解決這個問題的?我需要完成類似的場景 – MicFin

回答

0

所有其他的配置是相同的,變化如下:

1)openmeeting數據庫中創建用戶在after_create回調

models/user.rb

class User < ActiveRecord::Base 
# Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable, :confirmable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :new_password, :firstname, :lastname, :username, :email, :password, :password_confirmation, :remember_me, :confirmation_token 
    attr_accessor :new_password 

    # creating user record to another database just after Devise user is created 
    after_create :save_to_openmeetings 

    def save_to_openmeetings 
    OpenMeeting.create_user(self) 
    end 
end 

2)創建models/open_meeting.rb

class OpenMeeting < ActiveRecord::Base 

    # override establish_connection method 
    def self.establish_connection 
    super(:openmeetings_dev) 
    end 

    # create connection to your new database for me ```:openmeetings_dev``` 
    establish_connection 


    def self.create_user(user) 
    firstname = "#{ActiveRecord::Base::sanitize(user.firstname)}" # escape sql injection 
    lastname = "#{ActiveRecord::Base::sanitize(user.lastname)}" # escape sql injection 
    login = "#{ActiveRecord::Base::sanitize(user.email)}" # escape sql injection 
    password = "#{ActiveRecord::Base::sanitize(user.new_password)}" # escape sql injection 

    sql = ("INSERT INTO users (firstname, lastname, login, password) VALUES ('#{firstname}', '#{lastname}', '#{login}', '#{password}')") 
    connection.execute(sql) # execute create sql statement 
    end 

end 

該解決方案爲我工作,但我認爲,可能有比這更好的解決辦法,那麼,請後,如果有任何新的辦法解決這個問題

感謝