Posted By

tonic on 09/05/08


Tagged

rails ruby activerecord ror belongsto hasmany relacin relaciones


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Relacionar dos tablas por dos caminos diferentes


 / Published in: Rails
 

  1. #tablas
  2. noticias(id,titulo)
  3. boletins(id,portada_id,titulo)
  4. boletins_noticias(id,noticia_id,boletin_id)
  5.  
  6. #modelos
  7.  
  8. class Boletin < ActiveRecord::Base
  9. has_and_belongs_to_many :noticias
  10. belongs_to :portada, :foreign_key => :portada_id, :class_name => 'Noticia'
  11. end
  12.  
  13. class Noticia < ActiveRecord::Base
  14. has_and_belongs_to_many :boletins
  15. end
  16.  
  17. #pruebas en consola
  18. bole=Boletin.new(:titulo => 'Boletin numero uno')
  19. bole.save
  20. noti=Noticia.new(:titulo => 'Noticia numero uno')
  21. noti.save
  22. portada=Noticia.new(:titulo => 'Noticia de portada')
  23. portada.save
  24. bole.noticias << noti
  25. bole.portada=portada

Report this snippet  

You need to login to post a comment.