Ruby on Rails,habtm收集方法不收集?

发布于 2024-10-20 10:20:52 字数 510 浏览 1 评论 0原文

我正在尝试在删除时将数据备份到 yaml 存储,如下所示:

DeleteProject.create!(:data => {
  :project => project.attributes, 
  :domains => project.domains.collect(&:attributes), 
  :databases => project.databases.collect(&:attributes)
}.to_yaml)

但是,尽管项目和域数据存储良好,但数据库始终返回空白。该关系是通过

  • has_and_belongs_to_many :databases
  • has_and_belongs_to_many :projects

完成的我对此陷入了死胡同,因为我不知道为什么它根本不会传递当前数据来自项目.数据库。任何见解都会很棒,谢谢!

I am trying to backup data to a yaml store on deletion like so:

DeleteProject.create!(:data => {
  :project => project.attributes, 
  :domains => project.domains.collect(&:attributes), 
  :databases => project.databases.collect(&:attributes)
}.to_yaml)

However although the project and domain data is stored fine, databases is always coming back blank. The relationship is done via

  • has_and_belongs_to_many :databases
  • has_and_belongs_to_many :projects

I am at a dead end with this one as I do not know why it simply would not be passing in the current data from project.databases. Any insight would be great, thanks!

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

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

发布评论

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

评论(1

你的背包 2024-10-27 10:20:52

好的,我通过执行以下操作解决了这个问题:

  • has_many :database_connections
  • has_many :databases, :through =>
    :database_connections

更新模型以使用 has_many,然后更新连接表,不再是database_projects,而是database_connections。

class DatabaseConnection < ActiveRecord::Base
  belongs_to :projects, :class_name => "Project",
                        :foreign_key => "project_id"
  belongs_to :databases, :class_name => "Database",
                         :foreign_key => "database_id"
end

Okay so I fixed this by doing the following:

  • has_many :database_connections
  • has_many :databases, :through =>
    :database_connections

Updated models to use has_many and then updated the connecting table no longer database_projects but database_connections.

class DatabaseConnection < ActiveRecord::Base
  belongs_to :projects, :class_name => "Project",
                        :foreign_key => "project_id"
  belongs_to :databases, :class_name => "Database",
                         :foreign_key => "database_id"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文