通过嵌套表单中的复选框处理has_many

时间:2012-02-04 23:24:08

标签: ruby-on-rails ruby-on-rails-3

我正在尝试通过嵌套在父模型中的表单添加has_many_through关系。我无法弄清楚这是否可能?

嵌套表单在没有复选框的情况下正常工作。当复选框在嵌套表单之外时,复选框可以正常工作。

= semantic_form_for @raduser do |f|
  %ul       
    %li
      = f.text_field :expiration, :placeholder => 'Expiration Date', :id => 'expiration_date'
    - f.fields_for :radcheck do |builder|
      = builder.text_field :username, :placeholder => 'Stuff'
      -Radgroup.all.each do |group|
        %li
          = check_box_tag 'radcheck[radgroup_ids]', group.id, @radcheck.radgroup_ids.include?(group.id)
          = group.groupname          

    %li
      = f.submit

这会引发错误:

 undefined method `radgroup_ids' for #<ActiveRecord::Relation:0x007faaa24c2468>

我已经将模型设置得很好我认为所有值都可以在没有复选框的情况下完美保存

---编辑增加的模型---

Radusers有许多Radcheck(由于遗留数据库而单数)。 Radcheck通过radusergroup有许多radgroup。

class Raduser < ActiveRecord::Base

  has_many :radcheck, :dependent => :destroy
  accepts_nested_attributes_for :radcheck, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true   

end

class Radcheck < ActiveRecord::Base
  self.table_name = 'radcheck'

  attr_accessible :attribute_name, :username, :value, :op, :radgroupcheck_id, :radcheck_serial, :radgroup_ids

  belongs_to :raduser

  has_many :radusergroup, :dependent => :destroy
  has_many :radgroup, :through => :radusergroup

end

class Radgroup < ActiveRecord::Base
  self.table_name = 'radgroup'

  has_many :radusergroup, :dependent => :destroy
  has_many :radcheck, :through => :radusergroup

end


class Radusergroup < ActiveRecord::Base

belongs_to :radcheck
belongs_to :radgroup

end

- 更新两次 -

 def new
  @radcheck = Radcheck.new
  ...      
 end

 def create
  @radcheck = Radcheck.new(params[:radcheck])
  ...
 end

---更新3 ---

我的数据库目前看起来像这样:

 +----------------+-----------------------+----------+------+
 | radcheck_id    | radgroup_id           | priority | id   |
 +----------------+-----------------------+----------+------+
 | 8              | 3                     |        1 |    1 |
 | 4              | 9                     |        0 |    2 |
 | 22             | 4                     |        1 |    3 |
 | 2              | 6                     |        1 |    4 |

我的解决方案建议如下工作。

我希望做的是将radcheck_username和radgroup_groupname插入数据库,而不是radcheck_id:

+----------------+-----------------------+----------+------+
| ...username    | --groupname           | priority | id   |
+----------------+-----------------------+----------+------+
| user123344     | group88888            |        1 |    1 |
| shushQb        | 30-minutes            |        0 |    2 |
| forty          | gigiig                |        1 |    3 |
| snang          | ps-staff              |        1 |    4 |

这可能吗?

1 个答案:

答案 0 :(得分:1)

问题似乎在于您尝试通过尚未定义的方法访问Radcheck的各种Radgroup。我可以看到几个解决方案。一种方法是将缺少的 radgroup_ids 方法添加到模型中。或者,我认为你可以打电话给包括?在Radchecks radgroups 关系。

解决方案A

在Radcheck类中,定义:

def radgroup_ids
  radgroups.map(&:id)
end

解决方案B

对您检查包含的Radgroups

的方式稍作修改
@radcheck.radgroups.include?(group)