2016-03-29 3 views
1

私は1つだけ持っている場合、私は監査済 - ActiveRecordの宝石ポリモーフィック団体のために正常に動作していない

ユーザーhas_manyのアドレスは今

HomeAddressやOfficeAddress

のいずれか、すなわちているように、ネストされた団体のための監査を作りたいですテーブルアドレスと私はそれらを区別するためにタイプとIDを使用しています。この場合、私はassociated_auditsをUserに使用すると、ただ1つの監査レコードを作成し、レコードを再度更新します。ここで

は、モデルの協会である:

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

私は詳細を述べました。あなたは自宅の住所または事務所住所のネストされた属性で患者を更新し、あなたが患者を更新するたびに毎回1つの監査しかないでしょう。 – Jaswinder

+0

上記のバグがまだ残っている場合は、 'type'カラムを変更してみてください。それはレールに予約された名前であり、しばしばバグを引き起こします。 – 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

しかし、2つ以上の関連する監査がある場合は、他のモデル監査もグループ化されます – Jaswinder

+0

いいえ、グループ化されるまではグループ化されません。 – user2659613

+1

実際には、has_many:addresses'を使用しません。 1つのオフィスアドレスと1つのホームアドレスしか持たない。また、特定の列に対して監査を使用すると、実行中の移行時に破損します。 – Jaswinder

関連する問題