Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 0.91 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. destroy association instead of object
  2. class Category < ActiveRecord::Base
  3.   has_many :categorizations
  4.   has_many :projects, :through => :categorizations
  5.   attr_accessible :name
  6. end
  7.        
  8. class Categorization < ActiveRecord::Base
  9.     belongs_to :project
  10.     belongs_to :category
  11.     attr_accessible :project_id, :category_id
  12. end
  13.        
  14. class Project < ActiveRecord::Base
  15.   has_many :categorizations
  16.   has_many :categories, :through => :categorizations, :uniq => true
  17.   accepts_nested_attributes_for :categorizations, :allow_destroy => true
  18.   accepts_nested_attributes_for :categories, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? }
  19.   attr_accessible :name, :description, :content, :icon_id
  20.   attr_accessible :categories_attributes
  21. end
  22.        
  23. <p class="fields">
  24. <%= f.label :name, "Category" %>
  25. <%= f.text_field :name %>
  26. <%= f.hidden_field :_destroy %>
  27. <%= link_to_function "remove", "remove_fields(this)" %>
  28. </p>