2016-03-29 25 views
1

正常工作,我想使審計嵌套的協會,如我有審計,ActiveRecord的寶石不是多態關聯

用戶的has_many地址,即要麼

是homeAddress或OfficeAddress

現在,如果我只有一個表地址和我用類型和id來區分他們。在這種情況下,如果我使用associated_audits作爲用戶,那麼它只會創建一條審計記錄,並且每當我再次更新記錄時,它只是用最後一條記錄替換以前的審計記錄。

這裏是機型聯想:

class Patient < ActiveRecord::Base 

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, 
     :trackable, :validatable, :confirmable, request_keys: [:subdomain] 

    has_one :home_address,-> { where(addr_type: 'home') },class_name: 'Address' 
    has_one :office_address,-> { where(addr_type: 'office') }, class_name: 'Address' 
    has_associated_audits 

    accepts_nested_attributes_for :home_address, allow_destroy: true 
    accepts_nested_attributes_for :office_address, allow_destroy: true 
end 


class Address < ActiveRecord::Base 

    belongs_to :patient 
    audited 

end 
+0

「在,如果我使用的用戶associated_audits這種情況下,那麼它將使只有一個審計記錄」審計或地址?如果你需要幫助,請發佈你的模型和模式,不要假設人們會知道你在說什麼 – SomeSchmo

+0

我已經提到了細節。現在你可以更新患者的嵌套屬性爲家庭地址或辦公地址,並可以檢查每次更新患者時只會有一次審覈。 – Jaswinder

+0

如果您仍然從上面得到錯誤,您可能需要嘗試更改「類型」列。這是一個在rails中的保留名稱,通常會導致錯誤 – SomeSchmo

回答

1
class Address < ActiveRecord::Base 
    belongs_to :patient 
    audited associated_with: :patient 
end 

class Patient < ActiveRecord::Base 
    has_many :addresses 
    has_associated_audits 
end 
+1

但是,如果我們有兩個以上的相關審計,它也會將其他模型審計分組 – Jaswinder

+0

否它不會分組,直到您提及與哪個模型關聯.. – user2659613

+1

實際上,我不想使用'has_many:addresses'.Patient只有一個辦公地址和一個家庭住址。另外,當我們對任何特定列使用審計時,它將在運行遷移時中斷。 – Jaswinder