has_many autosave_associated_records_for_ * - PullRequest
4 голосов
/ 27 января 2012

Я пытаюсь связать существующие записи, но все еще не могу добавить новые.Следующее не работает, но довольно близко к тому, что мне нужно.Как мне выполнить связывание существующих записей и создание новых?

has_many :comments, :through => :commentings, :source => :commentable, :source_type => "Comment"
accepts_nested_attributes_for :comments, :allow_destroy => true

def autosave_associated_records_for_comments
  comments.each do |comment|
    if existing_comment = Comment.find_by_fax_and_name(comment.fax, comment.name)
      self.comments.reject! { |hl| hl.fax == existing_comment.fax && hl.name == existing_comment.name }
      self.comments << existing_comment
    else
      self.comments << comment
    end
  end
end

Вот соответствующая строка источника: https://github.com/rails/rails/blob/v3.0.11/activerecord/lib/active_record/autosave_association.rb#L155

Ответы [ 2 ]

6 голосов
/ 27 января 2012

Я нашел решение, но если вы знаете лучший способ сделать это, пожалуйста, дайте мне знать!

def autosave_associated_records_for_comments
  existing_comments = []
  new_comments = []

  comments.each do |comment|
    if existing_comment = Comment.find_by_fax_and_name(comment.fax, comment.name)
      existing_comments << existing_comment
    else
      new_comments << comment
    end
  end

  self.comments << new_comments + existing_comments
end
1 голос
/ 24 сентября 2014

У меня есть система тегов, которая использует отношение has_many: through. Ни одно из решений не дало мне того, куда мне нужно было идти, поэтому я нашел решение, которое может помочь другим. Это было проверено на Rails 3.2.

Настройка

Вот базовая версия моих моделей:

Местоположение объекта:

class Location < ActiveRecord::Base
    has_many :city_taggables, :as => :city_taggable, :dependent => :destroy
    has_many :city_tags, :through => :city_taggables

    accepts_nested_attributes_for :city_tags, :reject_if => :all_blank, allow_destroy: true
end

Метка объектов

class CityTaggable < ActiveRecord::Base
   belongs_to :city_tag
   belongs_to :city_taggable, :polymorphic => true
end

class CityTag < ActiveRecord::Base
   has_many :city_taggables, :dependent => :destroy
   has_many :ads, :through => :city_taggables
end

Решение

Я действительно переопределил метод autosave_associated_record_for следующим образом:

class Location < ActiveRecord::Base
   private

   def autosave_associated_records_for_city_tags
     tags =[]
     #For Each Tag
     city_tags.each do |tag|
       #Destroy Tag if set to _destroy
       if tag._destroy
         #remove tag from object don't destroy the tag
         self.city_tags.delete(tag)
         next
       end

       #Check if the tag we are saving is new (no ID passed)
       if tag.new_record?
         #Find existing tag or use new tag if not found
         tag = CityTag.find_by_label(tag.label) || StateTag.create(label: tag.label)
       else
         #If tag being saved has an ID then it exists we want to see if the label has changed
         #We find the record and compare explicitly, this saves us when we are removing tags.
         existing = CityTag.find_by_id(tag.id)
         if existing    
           #Tag labels are different so we want to find or create a new tag (rather than updating the exiting tag label)
           if tag.label != existing.label
             self.city_tags.delete(tag)
             tag = CityTag.find_by_label(tag.label) || CityTag.create(label: tag.label)
           end
         else
           #Looks like we are removing the tag and need to delete it from this object
           self.city_tags.delete(tag)
           next
         end
       end
       tags << tag
     end
     #Iterate through tags and add to my Location unless they are already associated.
     tags.each do |tag|
       unless tag.in? self.city_tags
         self.city_tags << tag
       end
     end
   end

Приведенная выше реализация сохраняет, удаляет и изменяет теги так, как мне было нужно при использовании fields_for во вложенной форме. Я открыт для обратной связи, если есть способы упростить. Важно отметить, что я изменяю теги при изменении метки, а не обновляю метку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...