'updated_at'(특히 원자 피드에서 사용하기 때문에)의 특성 때문에 레코드가있을 때 updated_at 필드를 업데이트하지 않아야합니다. 변경없이 저장됩니다. 이를 성취하기 위해 다음과 같이 읽었습니다.레일스가 has_many를 통해 저장할 때 save (인수 1은 0)에 오류가 발생합니다.
module ActiveRecord
class Base
before_validation :clear_empty_strings
# Do not actually save the model if no changes have occurred.
# Specifically this prevents updated_at from being changed
# when the user saves the item without actually doing anything.
# This especially helps when synchronizing models between apps.
def save
if changed?
super
else
class << self
def record_timestamps; false; end
end
super
class << self
remove_method :record_timestamps
end
end
end
# Strips and nils strings when necessary
def clear_empty_strings
attributes.each do |column, value|
if self[column].is_a?(String)
self[column].strip.present? || self[column] = nil
end
end
end
end
end
내 이메일 모델을 제외한 모든 모델에서 정상적으로 작동합니다. 이메일에는 많은 보낼 편지함이있을 수 있습니다. 발신 함은 기본적으로 구독자 (email To :)와 전자 메일 (subscriber to send to email)을 보유하는 2 열 모델입니다. 보낼 편지함의 속성을 업데이트 한 다음 전자 메일을 저장할 때 save (save 메서드의 'super'호출을 가리킴)에서 save (0 인수 1) 오류가 발생합니다.
Email.rb
has_many :outboxes, :order => "subscriber_id", :autosave => true
Outbox.rb
belongs_to :email, :inverse_of => :outboxes
belongs_to :subscriber, :inverse_of => :outboxes
validates_presence_of :subscriber_id, :email_id
attr_accessible :subscriber_id, :email_id
업데이트 : 나는 또한 내가 관련된 모델을 변경할 때 '변화'배열을 채워되지 않는 것으로 나타났습니다.
@email.outboxes.each do |out|
logger.info "Was: #{ out.paused }, now: #{ !free }"
out.paused = !free
end unless @email.outboxes.empty?
@email.save # Upon saving, the changed? method returns false...it should be true