0

'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 

답변

0

... 한숨. 솔루션을 찾으려고 무수한 시간을 보낸 후에 나는 this을 발견했습니다. 나는 'save'메소드가 실제로 이것을 알아 냈을 것이라는 주장을 실제로 취했다는 것을 알고 있었습니까? 분명히 source을 보면 그 점에 도움이되지 않았습니다. 이제는 save 메소드에 args = {} 매개 변수를 추가하고 'super'로 전달하면 모든 것이 작동합니다. 수정되지 않은 레코드는 타임 스탬프를 업데이트하지 않고 저장되며 수정 된 레코드는 타임 스탬프와 함께 저장되며 연결은 오류없이 저장됩니다.

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(args={}) 

    if changed? 
     super args 
    else 
     class << self 
     def record_timestamps; false; end 
     end 
     super args 
     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