我正在使用rails 4并且想要集成一些外部api,为此我必须同时在两个具有相同记录的数据库中创建用户。
我正在为这个 Rails 应用程序使用 Devise,到目前为止,我已完成以下操作以将用户记录保存在另一个数据库中:
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
问题 1.虽然它在两个数据库中正确创建用户(来自控制台日志),但我面临的问题是,数据存在于另一个数据库(openmeetings)中,但该数据在我的原始数据库(test_openmeetings_development)中不存在
- 连接未重置回原始数据库 (test_openmeetings_development)
可能我所做的一切都不是一个好主意,所以,请分享可以解决我的问题的逻辑。或者请让我知道这段代码中缺少什么(为什么它不起作用?)
谢谢
最佳答案
所有其他配置均相同,更改如下:
1) 在 after_create
回调中在 openmeeting 数据库中创建用户
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
这个解决方案对我有用,但我认为,可能有比这更好的解决方案,所以,如果这个问题有任何新的解决方案,请发布
谢谢
关于mysql - 同时在两个具有相同记录的数据库中创建 Devise 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085022/