通过 has_many 进行多个数据库连接

发布于 2024-12-18 16:47:48 字数 2046 浏览 1 评论 0原文

如何通过使用多个数据库连接来创建 has_many ?

我有一个名为“master”的数据库,其中保存位置信息。这是从单独的应用程序更新的。用户可以访问许多位置,但所有其他模型都位于另一个名为“budget”的数据库中。以下是模型的设置方式。

# place.rb
class Place < ActiveRecord::Base
  belongs_to :user
  belongs_to :location
end

# user.rb
class User < ActiveRecord::Base
  has_many :locations, :through => :places
  has_many :places
end

# location.rb
class Location < ActiveRecord::Base
  establish_connection "master"
  has_many :places
  has_many :users, :through => :places
end

当我通过 irb 运行命令时,我得到以下信息

> Location.first.places.create(:user_id => 1)
> #<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43",  updated_at: "2011-11-28 20:58:43">

> Location.first.places
> [#<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43", updated_at: "2011-11-28 20:58:43">]

> Location.first.users
> [#<User id: 1, username: "toby", role: "guest", created_at: "2011-11-28 17:45:40", updated_at: "2011-11-28 17:45:40">

> User.first.locations
> Mysql2::Error: Table 'master.places' doesn't exist: SELECT `locations`.* FROM `locations` INNER JOIN `places` ON `locations`.`id` = `places`.`location_id` WHERE `places`.`user_id` = 1 ActiveRecord::StatementInvalid: Mysql2::Error: Table 'master.places' doesn't exist: SELECT `locations`.* FROM `locations` INNER JOIN `places` ON `locations`.`id` = `places`.`location_id` WHERE `places`.`user_id` = 1

我尝试将当前的 Rails env 添加到 Place 来尝试覆盖 place 的默认数据库,如下所示: # 地点.rb 班级地点< ActiveRecord::基础 建立_连接Rails.env 属于:用户 属于:位置 这

#database.yml

master:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: master
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: budget_development
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

没有帮助。有什么想法吗?

How can I make a has_many through work with multiple database connections?

I have a database named "master" that holds the location information. That is updated from a separate application. Users can have access to many locations, but all the other models are located in another database named "budget". Here are how the models are setup.

# place.rb
class Place < ActiveRecord::Base
  belongs_to :user
  belongs_to :location
end

# user.rb
class User < ActiveRecord::Base
  has_many :locations, :through => :places
  has_many :places
end

# location.rb
class Location < ActiveRecord::Base
  establish_connection "master"
  has_many :places
  has_many :users, :through => :places
end

When I run commands through irb, I get the following

> Location.first.places.create(:user_id => 1)
> #<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43",  updated_at: "2011-11-28 20:58:43">

> Location.first.places
> [#<Place id: 1, user_id: 1, location_id: 1, created_at: "2011-11-28 20:58:43", updated_at: "2011-11-28 20:58:43">]

> Location.first.users
> [#<User id: 1, username: "toby", role: "guest", created_at: "2011-11-28 17:45:40", updated_at: "2011-11-28 17:45:40">

> User.first.locations
> Mysql2::Error: Table 'master.places' doesn't exist: SELECT `locations`.* FROM `locations` INNER JOIN `places` ON `locations`.`id` = `places`.`location_id` WHERE `places`.`user_id` = 1 ActiveRecord::StatementInvalid: Mysql2::Error: Table 'master.places' doesn't exist: SELECT `locations`.* FROM `locations` INNER JOIN `places` ON `locations`.`id` = `places`.`location_id` WHERE `places`.`user_id` = 1

I tried adding the current rails env to Place to try and override the default database for place, like this:
# place.rb
class Place < ActiveRecord::Base
establish_connection Rails.env
belongs_to :user
belongs_to :location
end

#database.yml

master:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: master
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: budget_development
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

That didn't help. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-12-25 16:47:48

一位朋友帮我回答了这个问题,我想这可能对其他人有用。

class Location < ActiveRecord::Base
  #establish_connection "master"
  def self.table_name() "master.locations" end
  has_many :places
  has_many :users, :through => :places
end

A friend answered this for me, and I figured it might be of some use to others.

class Location < ActiveRecord::Base
  #establish_connection "master"
  def self.table_name() "master.locations" end
  has_many :places
  has_many :users, :through => :places
end
最笨的告白 2024-12-25 16:47:48

答案对我有用,但我在关系表中使用这个版本:

self.table_name = "master.locations"

The answer works for me, but I use this version in my relation table:

self.table_name = "master.locations"
没︽人懂的悲伤 2024-12-25 16:47:48

几年后阅读本文的任何人都请注意,Rails 7 已为 has_onehas_many 引入了显式 disable_joins: true 选项。

参考: https://edgeguides.rubyonrails.org/ active_record_multiple_databases.html#handling-associations-with-joins-across-databases

Anyone reading this a few years later, please be advised Rails 7 has introduced an explicit disable_joins: true option to has_one and has_many.

ref: https://edgeguides.rubyonrails.org/active_record_multiple_databases.html#handling-associations-with-joins-across-databases

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文