2017-04-11 115 views
0

在一個簡化的設置中,我有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

回答

0

:belongs_to可以是可選的,我們只需追加',可選:真',你可以保存記錄到你的分貝沒有任何問題。但要小心,即將對象置於無效狀態(從業務邏輯角度來看),不要忘記更新保存的記錄並添加外部id值。

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

感謝您的答案,但是我知道可選,我不想處理模型的可能出錯的狀態。該解決方案還需要先保存變量和環境,然後再保存VariableValues – bmeyers