简体   繁体   English

与活动记录交叉数据库连接

[英]Cross Database Connection with Active Record

I have two websites that each connect to their own unique databases. 我有两个网站,每个网站都连接到自己独特的数据库。 I need to validate in website 'A' that an email address exists in the website 'B' database. 我需要在网站“A”中验证网站“B”数据库中是否存在电子邮件地址。 I'm doing the validation as follows: 我正在进行如下验证:

Called from website 'A's AccountController < ApplicationController class: 从网站'A的AccountController < ApplicationController类调用:

config = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml"))
      ActiveRecord::Base.establish_connection(config["database B"])
      if ActiveRecord::Base.connection.select_values("SELECT "database B".X
          FROM 'database B".X WHERE 'database B'.X.email = @member_email")

This call works when I test it in my development and QA environments but fails in my production environment. 当我在开发和QA环境中测试它但在我的生产环境中失败时,此调用有效。 What appears to happen in production is that the value of the ActiveRecord and also the select get's co-mingled with currently logged-in user's active records, but only in production. 生产中似乎发生的事情是ActiveRecord的值以及select get与当前登录用户的活动记录混合,但仅限于生产中。

Okay so I've modified my files to the following, based on the feedback. 好的,所以我根据反馈将我的文件修改为以下内容。 Still not working... Could someone please review the files below and see what step(s) I'm missing? 仍然没有工作......有人可以查看下面的文件,看看我错过了哪些步骤? Thanks! 谢谢!

Thanks! 谢谢! I think that is what I did, but I created the 'model', and, being a newbie, I'm not sure if that would normally be generated by Rails... 我认为这就是我所做的,但我创造了'模型',并且,作为一个新手,我不确定这通常是由Rails生成的......

Still failing, would you mind taking a look at the following and see if you see what I'm doing wrong? 仍然失败,你介意看看下面的内容,看看你是否看到我做错了什么?

First, this is the 'legacy' database model for the second database that I want to connect to in the existing application (Note that doing the 'Fileload' was the only way I could get this to not error out.): 首先,这是我想要在现有应用程序中连接的第二个数据库的“遗留”数据库模型(请注意,执行'Fileload'是我唯一可以解决此问题的方法。):

class MMSDB < ActiveRecord::Base

self.abstract_class = true #important! self.abstract_class = true #important!

config = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) config = YAML :: load(File.open(“#{RAILS_ROOT} /config/database.yml”))

establish_connection(config["deals_qa"]) establish_connection(CONFIG [ “deals_qa”])

end 结束

Second, this is the model that calls the 'MMSDB' Model (see above) 其次,这是调用'MMSDB'模型的模型(见上文)

class Redirect < MMSDB

def initialize def初始化

end 结束

Checking to see if the email address exists in the legacy database, and, if it does, the #increment the redirect count on the # database table 'members' 检查旧数据库中是否存在电子邮件地址,如果存在,则#increment #database表'成员'上的重定向计数

Do I somehow need to tell the application what table I want to pull from since the table # in the legacy database (members) would be different then in the current application #database (users) 我是否需要告诉应用程序我要从哪个表中提取,因为遗留数据库(成员)中的表#将与当前应用程序中的#database(用户)不同

def email_exists?(email) if find_by_email(email) user = find_by_email(email) user.redirect_count += 1 user.save end end def email_exists?(email)if find_by_email(email)user = find_by_email(email)user.redirect_count + = 1 user.save end end

end 结束

Then this is the code snippet inside the account controller file. 然后,这是帐户控制器文件中的代码段。

        else
        if user == Redirect::User.email_exists?(@email)
            @Redirect_Flag = true
        else
            flash.now[:error] = 'Invalid email or password; please try again.'
        end
     end

Subclassing ActiveRecord::Base will allow you to make multiple connections to different databases. 子类化ActiveRecord :: Base将允许您与不同的数据库建立多个连接。

module DatabaseB
  class Base < ActiveRecord::Base
    @abstract_class = true
    establish_connection(config["database B"])
  end
end

class YourAwesomeModel < DatabaseB::Base
  set_table_name "X"
  # Use your regular active record magic
end

You will still be able to use your other models with the connection established using ActiveRecord::Base to your primary database. 您仍然可以将其他模型与使用ActiveRecord :: Base建立的连接一起用于主数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM