问题描述
我的环境: Ruby 1.9.2p290,Rails 3.0.9和Rubygem 1.8.8
不幸的是,我遇到多个数据库时有问题.
情况是:我有两个模型与两个不同的数据库连接,并且还建立了彼此之间的关联. 在每个模型中指定的数据库连接,看起来喜欢
class Visit < ActiveRecord::Base self.establish_connection "lab" belongs_to :patient end class Patient < ActiveRecord::Base self.establish_connection "main" has_many :visits end
我会在以下情况下遇到错误
@visits = Visit.joins(:patient)
错误:mysql2 ::错误:table'lab.patients'不存在:select visits.
此处的"患者"表位于"主"数据库中,"实验室"数据库中的"访问"表 我怀疑何时执行代码时,Rails正在考虑"患者"表是"实验室"数据库的一部分[保留"访问"表的表].
推荐答案
好吧,我不知道这是否是最优雅的解决方案,但是我确实通过定义self.table_name_prefix来明确返回数据库名称来工作.
class Visit < ActiveRecord::Base def self.table_name_prefix renv = ENV['RAILS_ENV'] || ENV['RACK_ENV'] (renv.empty? ? "lab." : "lab_#{renv}.") end self.establish_connection "lab" belongs_to :patient end class Patient < ActiveRecord::Base def self.table_name_prefix renv = ENV['RAILS_ENV'] || ENV['RACK_ENV'] (renv.empty? ? "main." : "main_#{renv}.") end self.establish_connection "main" has_many :visits end
在指定联接条件时,我仍在努力详细介绍,但我希望这会有所帮助.
其他推荐答案
做这样的事情可能更干净:
def self.table_name_prefix "#{Rails.configuration.database_configuration["#{Rails.env}"]['database']}." end
将从您的数据库中获取适当的数据库名称
其他推荐答案
甚至
def self.table_name_prefix self.connection.current_database+'.' end
问题描述
My environment: Ruby 1.9.2p290, Rails 3.0.9 and RubyGem 1.8.8
unfortunately I have an issue when come across multiple database.
The situation is this: I have two model connect with two different database and also establishing association between each other. database connection specifying in each model, look likes
class Visit < ActiveRecord::Base self.establish_connection "lab" belongs_to :patient end class Patient < ActiveRecord::Base self.establish_connection "main" has_many :visits end
I got an error when meet following scenario
@visits = Visit.joins(:patient)
Errors: Mysql2::Error: Table 'lab.patients' doesn't exist: SELECT visits.* FROM visits INNER JOIN patients ON patients.id IS NULL
Here 'patients' table is in 'main' database and 'visits' table in 'lab' database I doubt when executing the code, that Rails is considering 'patients' table is part of 'lab' database [which holds 'visits' table].
推荐答案
Well, I don't know if this is the most elegant solution, but I did get this to work by defining self.table_name_prefix to explicitly return the database name.
class Visit < ActiveRecord::Base def self.table_name_prefix renv = ENV['RAILS_ENV'] || ENV['RACK_ENV'] (renv.empty? ? "lab." : "lab_#{renv}.") end self.establish_connection "lab" belongs_to :patient end class Patient < ActiveRecord::Base def self.table_name_prefix renv = ENV['RAILS_ENV'] || ENV['RACK_ENV'] (renv.empty? ? "main." : "main_#{renv}.") end self.establish_connection "main" has_many :visits end
I'm still working through all the details when it comes to specifying the join conditions, but I hope this helps.
其他推荐答案
Might be cleaner to do something like this:
def self.table_name_prefix "#{Rails.configuration.database_configuration["#{Rails.env}"]['database']}." end
That will pull the appropriate database name from your database.yml file
其他推荐答案
Or even
def self.table_name_prefix self.connection.current_database+'.' end