Re: [Ror-es] Auto-relación simétrica

Sergio Gil Pérez de la Manga
Wed Feb 15 10:01:39 GMT 2006


El día 14/02/06, Diego Algorta Casamayou  escribió:

No tengo demasiada experiencia todavía, pero creo que podrías usar un
> filtro before_save().
>

Por si a alguien le viene bien, éste es el código (parece que funciona
bien):


class Contact < ActiveRecord::Base

  [validaciones y otras relaciones]

  has_and_belongs_to_many :contacts, :foreign_key => "related"

  def before_save
    for contact in self.contacts
      if !contact.contacts.include?(self)
        contact.contacts << self
        contact.save
      end
    end
  end
end

--
Sergio Gil Pérez de la Manga 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://barclay.textdrive.com/pipermail/ror-es/attachments/20060215/c5774a4d/attachment.htm