我正在使用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)
可能是我正在做的事情不是一个好主意,所以,请分享我可以解决我的问题的逻辑。或者,请告诉我这段代码中缺少的内容(为什么它不起作用?)
由于
答案 0 :(得分:0)
所有其他配置都相同,更改如下:
1)在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
此解决方案对我有用,但我认为,可能有更好的解决方案,所以,如果有任何新的解决方案,请发布此问题
由于