【发布时间】:2019-04-07 09:19:35
【问题描述】:
我有一个 rails(3) 应用程序(Master),它具有架构、模型和数据库。此外,我还有另一个 rails(5) app(service) 没有任何模型,但我正在使用(主)应用程序数据库。
服务应用配置
common_db:
adapter: postgresql
database: 'blog'
username: 'username'
password: 'password'
host: 'localhost'
pool: '10'
application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
establish_connection
Rails.configuration.database_configuration["common_db"]
end
然后我在服务应用程序中添加了一个相同的模型,然后尝试从主应用程序获取一些值
class post < ApplicationRecord
has_many :comments
scope :article_comments, lambda { |a| select("created_at").joins('INNER JOIN comments on post.id = comments.post_id').where('article_id = ?',
a) }
end
如果我在这里调用 Post.article_cmets 加入,但一切都不起作用,这绝对不起作用,因为我们在服务应用程序中没有任何关联或任何模型。
基本上我的问题是
1) 我可以在服务应用中重复使用这个主数据库模型吗?
2) 如果我可以使用它,那么我可以将它与所有关联一起使用吗 例子: 服务应用程序中的 Blog.cmets .....
3) 还有没有其他好的方法可以重用这个,我也看到了一些文章 https://hiltmon.com/blog/2013/10/14/rails-tricks-sharing-the-model/,这将是一件好事吗?
【问题讨论】:
-
究竟是什么错误?
-
我们无法使用关联,这是问题
标签: ruby-on-rails postgresql heroku multi-tenant