Relacionar dos tablas por dos caminos diferentes


/ Published in: Rails
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.