Update a list of model instances and their associations

I have been unable to figure out how to update a list of model
instances and their associations and I have looked in the
documentation, which doesnt give an example of what I am trying to do:

I would assume this is a common task though.

I have an established association:

class AlertRule < ActiveRecord::Base

has_many :notification_emails, :dependent => :destroy
accepts_nested_attributes_for :notification_emails


class NotificationEmail < ActiveRecord::Base
belongs_to :alert_rule


def alerts_config

  //more code
  @alert_rules = alert_rules.flatten
  @alert_rules.each { |a| a.notification_emails.build }

  render :partial => 'home/alerts_config', :layout => false


= form_tag ‘/home/save_alerts_config/’ + @unit.id.to_s, :remote =>
true, :class => ‘ajaxForm’ do
%table.scrollTable{:cellspacing => “0”, :width =>
“740px”, :style => “border-collapse: collapse; border-spacing: 0;”}
%th Alerts
%th Enable
%th Primary Email
%th Notification Emails
- for rule in @alert_rules
= fields_for :alert_rules, rule do |f|
%td= rule.alert_code.name
%td= check_box_tag “enabled_ids[]”, rule.id
%td= f.text_field :email, :value => rule.email, :index
=> rule.id
= f.fields_for :notification_emails do |
= notification_builder.text_field :email
notification_builder.hidden_field :_destroy
= link_to_function ‘Remove Notification’,
= submit_tag “Save”, :class => ‘submit myButton’

I look at the name attribute of one of the notification email fields,
and it looks like this:

However, it should look like this:

So what is missing is the association between this alert rule and its
notification emails. I am not sure how to make it look like the above.
But because it doesnt contain the id in brackets of the alert rule,
when I post, i get this back:

Started POST “/home/save_alerts_config/6243” for at
2012-03-21 15:12:22 -0400
Processing by HomeController#save_alerts_config as
Parameters: {“utf8”=>“✓”,
“enabled_ids”=>[“51”, “52”], “alert_rules”=>{“51”=>{“email”=>“hythy”},
“_destroy”=>“false”}}, “52”=>{“email”=>“yutu”},
“53”=>{“email”=>“ytuytu”}, “54”=>{“email”=>""}, “55”=>{“email”=>""},
“56”=>{“email”=>""}, “57”=>{“email”=>""}, “58”=>{“email”=>""},
“59”=>{“email”=>""}, “60”=>{“email”=>""}, “61”=>{“email”=>""},
“62”=>{“email”=>""}, “63”=>{“email”=>""}, “64”=>{“email”=>""},
“65”=>{“email”=>""}, “66”=>{“email”=>""}, “67”=>{“email”=>""},
“68”=>{“email”=>""}, “69”=>{“email”=>""}, “70”=>{“email”=>""},
“71”=>{“email”=>""}, “72”=>{“email”=>""}, “73”=>{“email”=>""},
“74”=>{“email”=>""}, “75”=>{“email”=>""}, “76”=>{“email”=>""},
“77”=>{“email”=>""}, “78”=>{“email”=>""}, “79”=>{“email”=>""},
“80”=>{“email”=>""}, “81”=>{“email”=>""}, “82”=>{“email”=>""},
“83”=>{“email”=>""}, “84”=>{“email”=>""}, “85”=>{“email”=>""},
“86”=>{“email”=>""}, “87”=>{“email”=>""}, “88”=>{“email”=>""},
“89”=>{“email”=>""}, “90”=>{“email”=>""}, “91”=>{“email”=>""}},

It has this:


thats supposed to be another alert rule, not a notification email of
alert rule 51, and this occurs because the name attribute is not
including the id of the alert rule of the notification email.

thanks at all for any possible workaround for this

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs