Guest User

Untitled

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Product < ActiveRecord::Base
  2. has_many :announcement_products
  3. has_many :announcements, :through => :announcement_products
  4.  
  5. accepts_nested_attributes_for :announcements, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
  6. end
  7.  
  8. class Announcement < ActiveRecord::Base
  9. has_many :announcement_products
  10. has_many :products, :through => :announcement_products
  11.  
  12. accepts_nested_attributes_for :products, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
  13. end
  14.  
  15. class AnnouncementProduct < ActiveRecord::Base
  16. belongs_to :announcement
  17. belongs_to :product
  18. end
  19.  
  20. -------------
  21. CONSOLE
  22.  
  23. a = Announcement.new()
  24. a.products << Product.first
  25. a.products << Product.last
  26. a.name = 'foo'
  27. a.description = 'bar'
  28. a.save!
Add Comment
Please, Sign In to add comment