如何在未创建关联模型时处理创建连接模型?

问题描述:

在一个简化的设置中,我有4个模型。如何在未创建关联模型时处理创建连接模型?

  • 应用:的has_many环境变量
  • 环境:的has_many VariableValues
  • 变量:的has_many VariableValues
  • VariableValue:belongs_to的环境变量

在我反应过来的应用程序,您可以创建一个应用程序,那么你可以独立创建Envrionments和Variables。每个变量都可以具有每个环境的值,因此我创建了一个具有实际值的VariableValue,然后创建了一个将变量值与环境相关联的environment_id和variable_id。当保存这个,并且一次保存时,我没有environment_id给连接模型,因为变量或环境在创建VariableValues之前都不会被保存。我正在用接受_nested_attributes完成应用程序的整个保存。这可能是不可能的,所以我很好奇别人是如何处理这种情况的。

谢谢!

UPDATE:

这里是我的模型

class Workspace < ApplicationRecord 
    has_many :workspace_envs, inverse_of: :workspace 
    has_many :workspace_variables, inverse_of: :workspace 
    accepts_nested_attributes_for :workspace_envs, :workspace_variables, allow_destroy: true 
end 

class WorkspaceEnv < ApplicationRecord 
    acts_as_paranoid 
    belongs_to :workspace, inverse_of: :workspace_envs 

    has_many :workspace_env_variable_values, inverse_of: :workspace_env 
    has_many :workspace_variable_values, inverse_of: :workspace_env, through: :workspace_env_variable_values 
    has_many :workspace_variables, inverse_of: :workspace_envs, through: :workspace_env_variable_values 


end 

class WorkspaceVariable < ApplicationRecord 
    belongs_to :workspace, inverse_of: :workspace_variables 
    has_many :workspace_env_variable_values, inverse_of: :workspace_variable 
    has_many :workspace_variable_values, inverse_of: :workspace_variable, through: :workspace_env_variable_values, dependent: :destroy 
    has_many :workspace_envs, inverse_of: :workspace_variables, through: :workspace_env_variable_values 

    accepts_nested_attributes_for :workspace_variable_values, allow_destroy: true 

end 

class WorkspaceVariableValue < ApplicationRecord 
    include VariableValue 

    has_one :workspace_env_variable_value, inverse_of: :workspace_variable_value 
    has_one :workspace_variable, inverse_of: :workspace_variable_values, through: :workspace_env_variable_value 
    has_one :workspace_env, inverse_of: :workspace_variable_values, through: :workspace_env_variable_value 

    accepts_nested_attributes_for :workspace_env 

end 


class WorkspaceEnvVariableValue < ApplicationRecord 
    belongs_to :workspace_env, inverse_of: :workspace_env_variable_values 
    belongs_to :workspace_variable_value, inverse_of: :workspace_env_variable_value 
    belongs_to :workspace_variable, inverse_of: :workspace_env_variable_values 
end 

这里最重要的是,所有这些车型都在同一时间创建的,甚至没有工作空间存在,它是所有的大拯救在一个大事务中,我目前的解决方案是构建workspace_variables和workspace_envs,然后根据其索引将workspace_envs和workspace_env_variables_values相关联。

+0

你可以发布你的模型代码? – Jeremie

+0

一个解决方案是使用UUID键而不是顺序整数。这允许前端为新记录生成密钥。请参见[this](http://blog.arkency.com/2014/10/how-to-start-using-uuid-in-activerecord-with-postgresql/)和[this](http://blog.bigbinary .com/2016/04/04/rails-5-provides-application-config-to-use -UUID-as-primary-key.html)以获取更多详细信息。 – moveson

+0

@moveson这当然是一种可能性,甚至在没有实际使用UUID的情况下,我考虑过给它们一个临时UID的想法,然后在保存workspace_envs之后,创建一个UID到ID的映射,然后创建与连接的关联模型 – bmeyers

:belongs_to可以是可选的,我们只需追加',可选:真',你可以保存记录到你的分贝没有任何问题。但要小心,即将对象置于无效状态(从业务逻辑角度来看),不要忘记更新保存的记录并添加外部id值。

class VariableValue < ApplicationRecord 
    belongs_to :environment, optional: true 
    belongs_to :variable, optional: true 
+0

感谢您的答案,但是我知道可选,我不想处理模型的可能出错的状态。该解决方案还需要先保存变量和环境,然后再保存VariableValues – bmeyers