0

继续尝试在 Rails 的一个应用程序中连接多个数据库的能力。这种能力是一个特定的生产要求,我是一个完整的新手......

所以我接受了之前的建议。请注意,我无法让应用程序从 database.yml 中读取“deals_qa”环境,或者至少它是这样出现的。

现在,最终结果是将我发送到应用程序生成的“糟糕”页面,我假设创建该页面是为了解决任何错误,因此没有具体说明为什么这不起作用。

请查看并提供任何建议,或我应该考虑的其他事项以使其正常工作...谢谢!

以下是基于先前建议的代码更改:

这是名为“dealsdb”的新模型文件:

module Dealsdb

@abstract_class = true

conn = { :adapter => 'mysql',
 :encoding => 'utf8',
 :reconnect => 'true',
 :database => 'deals_qa',
 :username => 'xxxxxx',
 :password => 'xxxxxx',
 :host => 'xx.xx.xx.xx',
 :port => 'xxxx'
}

establish_connection(conn["deals_qa"])

end
end

class Members < Dealsdb::Base

  def email_exists?(email)
    unless email.blank? || SYSTEM_EMAILS.include?(email)
    returning find_by_email(email) do |user|
    end
    end
  end
end

这是现有 Account Controller 文件中对应的代码片段,文件名为 Account_Controller.rb。请注意,我只是想克服第一个条件。

if Dealsdb::Members.email_exists?(@email)
            @Redirect_Flag = true
        else
            flash.now[:error] = 'Didnt MMS database check didnt work'
        end

谢谢你!

4

2 回答 2

0

以前有一个问题是关于在 Rails 中连接到多个数据库

具有多个数据库的 Rails RSpec

我引用了 Rspec,但该模式通常有效,并且我已经成功使用过很多次。

于 2012-10-07T13:30:47.823 回答
0

你没有任何继承自ActiveRecord::Base. 我这样做的方法是让我的模型继承自ActiveRecord::Base(rails 中的规范)。然后在模型内部更改连接establish_connection(db_name)

例子:

class Members < ActiveRecord::Base
  establish_connection(:foo)

  def email_exists?(email)
    unless email.blank? || SYSTEM_EMAILS.include?(email)
    returning find_by_email(email) do |user|
    end
    end
  end
end

在 database.yml 中:

foo:
  adapter: mysql
  host: localhost
  database: deals_qa
  username: xxxxxx
  password: xxxxxx

这是我找到的参考链接:https ://groups.google.com/forum/?fromgroups=#!topic/rubyonrails-talk/OpHh6FdNELo

于 2012-10-07T08:38:18.117 回答