사용 된 값에 액세스하지만, 제가 당신이하려고 생각의 예를 제공하도록 할 수 있습니다.
아래 코드는 나를 위해 잘 작동 : 상호 작용하는 경우는 다음과 같은 출력 결과
class Person < ApplicationRecord
has_many :addresses
validates_presence_of :name
before_save { puts "before_save of person - changes: #{changes}" }
before_destroy { puts "before_destroy of person with id: #{id}" }
end
class Address < ApplicationRecord
belongs_to :person, required: true
validates_presence_of :name
before_save { puts "before_save of address - changes: #{changes}" }
before_destroy { puts "before_destroy of address with id: #{id}" }
end
:
person = Person.create(name: 'Johan Wentholt')
# before_save of person - changes: {"name" =>[nil, "Johan Wentholt"]}
#=> #<Person id: 2, name: "Johan Wentholt", created_at: "2017-10-25 15:04:27", updated_at: "2017-10-25 15:04:27">
person.addresses.create(name: 'Address #1')
# before_save of address - changes: {"person_id"=>[nil, 2], "name" =>[nil, "Address #1"]}
#=> #<Address id: 7, person_id: 2, name: "Address #1", created_at: "2017-10-25 15:06:38", updated_at: "2017-10-25 15:06:38">
person.addresses.last.update(name: 'Address without typo')
# before_save of address - changes: {"name"=>["Address #1", "Address without typo"]}
#=> true
person.update(name: 'Kaasboer')
# before_save of person - changes: {"name"=>["Johan Wentholt", "Kaasboer"]}
#=> true
person.addresses.last.destroy
# before_destroy of address with id: 7
#=> #<Address id: 7, person_id: 2, name: "Address without typo", created_at: "2017-10-25 15:06:38", updated_at: "2017-10-25 15:08:51">
person.destroy
# before_destroy of person with id: 2
#=> #<Person id: 2, name: "Kaasboer", created_at: "2017-10-25 15:04:27", updated_at: "2017-10-25 15:10:46">
을이 모든 변경 사항을 기록합니다 볼 수 있듯이. 제가 말했듯이, 질문은 약간 불분명하지만, 이것이 당신을 더 멀리하는 데 도움이되기를 바랍니다.
일부 Rails 메서드는 콜백을 트리거하지 않습니다. 예 : , update_all, update_column 등을 삭제하십시오. 변경 사항에 대한 자세한 내용은
이 한 번 봐 걸릴 :
이 책 레코드에
class Book < ActiveRecord::Base
unloadable
has_many :titles, dependent: :destroy
has_many :authors, :through => :titles
accepts_nested_attributes_for :authors
before_save :pre_save
after_save :post_save
before_destroy :pre_delete
def pre_save
@nr = self.new_record?
end
def pre_save
changed_values = []
if @nr
changed_values.push "New record created"
else
self.changes.each do |field, cvs|
changes.push("#{field} : #{cvs[0]} => #{cvs[1]}")
end
end
if changes.length > 0
BookLog.create(:book_id => self.id, :changed_values => changes.join(', '))
end
end
def pre_delete
BookLog.create(:book_id => self.id, :changed_values => "Deleted: #{self.name}")
end
end
class Title < ActiveRecord::Base
unloadable
belongs_to :book
belongs_to :author
end
class Author < ActiveRecord::Base
unloadable
has_many :titles, dependent: :destroy
has_many :books, :through => :titles
accepts_nested_attributes_for :books
end
class BooksController < ApplicationController
def edit
book = Book.find(params[:book][:id])
book.name = .....
===> Here the old values are still available <====
book.author_ids = params[:book][:author_ids]
===> Now the new values are written to the database! <====
book.save!
end
end
변경은 (는) 도심에 기록됩니다 선명도를 위해서 ActiveModel::Dirty
업데이트하기 전에 old_value를 원하지만 new_Value가 올바르게 표시되고 있습니까? – krishnar
@krishnar : 예, 연결이 자체 테이블에 기록되므로 이전 값은 관련 테이블에서 삭제 된 것으로 보이는 레코드입니다. – GTeley
음, 이전 값을 얻는 한 가지 방법을 찾았습니다. 기존 레코드에 새 값을 할당하기 직전에 컨트롤러 저장 메소드에서. 그러나 이것은 의도 된 레일스 방법이 아닌 IMHO입니다! – GTeley