开发者

Ruby on Rails , habtm collect method not collecting?

开发者 https://www.devze.com 2023-02-14 23:31 出处:网络
I am trying to backup data to a yaml store on deletion like so: DeleteProject.create!(:data => { :project => project.attributes,

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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消