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

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.41 KB  |  hits: 23  |  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. Dealing with has_many through checkboxes in a nested form
  2. = semantic_form_for @raduser do |f|
  3.   %ul      
  4.     %li
  5.       = f.text_field :expiration, :placeholder => 'Expiration Date', :id => 'expiration_date'
  6.     - f.fields_for :radcheck do |builder|
  7.       = builder.text_field :username, :placeholder => 'Stuff'
  8.       -Radgroup.all.each do |group|
  9.         %li
  10.           = check_box_tag 'radcheck[radgroup_ids]', group.id, @radcheck.radgroup_ids.include?(group.id)
  11.           = group.groupname          
  12.  
  13.     %li
  14.       = f.submit
  15.        
  16. undefined method `radgroup_ids' for #<ActiveRecord::Relation:0x007faaa24c2468>
  17.        
  18. class Raduser < ActiveRecord::Base
  19.  
  20.   has_many :radcheck, :dependent => :destroy
  21.   accepts_nested_attributes_for :radcheck, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true  
  22.  
  23. end
  24.  
  25. class Radcheck < ActiveRecord::Base
  26.   self.table_name = 'radcheck'
  27.  
  28.   attr_accessible :attribute_name, :username, :value, :op, :radgroupcheck_id, :radcheck_serial, :radgroup_ids
  29.  
  30.   belongs_to :raduser
  31.  
  32.   has_many :radusergroup, :dependent => :destroy
  33.   has_many :radgroup, :through => :radusergroup
  34.  
  35. end
  36.  
  37. class Radgroup < ActiveRecord::Base
  38.   self.table_name = 'radgroup'
  39.  
  40.   has_many :radusergroup, :dependent => :destroy
  41.   has_many :radcheck, :through => :radusergroup
  42.  
  43. end
  44.  
  45.  
  46. class Radusergroup < ActiveRecord::Base
  47.  
  48. belongs_to :radcheck
  49. belongs_to :radgroup
  50.  
  51. end
  52.        
  53. def new
  54.   @radcheck = Radcheck.new
  55.   ...      
  56.  end
  57.  
  58.  def create
  59.   @radcheck = Radcheck.new(params[:radcheck])
  60.   ...
  61.  end
  62.        
  63. +----------------+-----------------------+----------+------+
  64.  | radcheck_id    | radgroup_id           | priority | id   |
  65.  +----------------+-----------------------+----------+------+
  66.  | 8              | 3                     |        1 |    1 |
  67.  | 4              | 9                     |        0 |    2 |
  68.  | 22             | 4                     |        1 |    3 |
  69.  | 2              | 6                     |        1 |    4 |
  70.        
  71. +----------------+-----------------------+----------+------+
  72. | ...username    | --groupname           | priority | id   |
  73. +----------------+-----------------------+----------+------+
  74. | user123344     | group88888            |        1 |    1 |
  75. | shushQb        | 30-minutes            |        0 |    2 |
  76. | forty          | gigiig                |        1 |    3 |
  77. | snang          | ps-staff              |        1 |    4 |
  78.        
  79. def radgroup_ids
  80.   radgroups.map(&:id)
  81. end
  82.        
  83. @radcheck.radgroups.include?(group)