Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7675169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:54:03+00:00 2026-05-31T16:54:03+00:00

I have a form that enables a user to update multiple alert rules records

  • 0

I have a form that enables a user to update multiple alert rules records at the same time. But each alert rule record can have many notification emails. So I would like to be able to update alert rules and the associated notification emails within the same form.

Models:

class AlertRule < ActiveRecord::Base

    has_many :notification_emails, :dependent => :destroy
    accepts_nested_attributes_for :notification_emails, :reject_if => lambda { |notification| notification[:email].blank? }
end

class NotificationEmail < ActiveRecord::Base
  belongs_to :alert_rule
end

In one of my controllers, I send an array of alerts to a form:

  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
  else

Then in my form, I want to allow the user to update alert rules and the associated email notifications:

    = form_for @alert_rules, :url => '/home/save_alerts_config/' + @unit.id.to_s, :remote => true, :class => 'ajaxForm' do |f|
  %table.scrollTable{:cellspacing => "0", :width => "740px", :style => "border-collapse: collapse; border-spacing: 0;"}
    %thead.fixedHeader
      %tr
        %th Alerts
        %th Enable
        %th Primary Email
        %th Notification Emails
        %th
    %tbody.scrollContent
      - for rule in @alert_rules
        %tr
          %td= rule.alert_code.name
          %td= check_box_tag "enabled_ids[]", rule.id
          %td= f.text_field :email, :value => rule.email, :index => rule.id
          %td.fields
            = f.fields_for :notification_emails, rule.notification_emails do |notification_builder|
              = notification_builder.text_field :email
              = notification_builder.hidden_field :_destroy              
              = link_to_function 'Remove Notification', 'remove_notifications(this)'
  .save_panel
    = submit_tag "Save", :class => 'submit myButton'

When I submit to server, this is what I get:

Started POST "/home/save_alerts_config/6243" for 127.0.0.1 at 2012-03-20 17:49:06 -0400
  Processing by HomeController#save_alerts_config as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"NPwuKuWippYjm2tJcfQI+/x9oEBwcR2rxcfpZMTO/Qo=", "enabled_ids"=>["51"], "alert_rule"=>{"51"=>{"email"=>"hythy"}, "notification_emails_attributes"=>{"0"=>{"email"=>"rtyrytry", "_destroy"=>"false"}, "1"=>{"email"=>"", "_destroy"=>"false"}, "2"=>{"email"=>"", "_destroy"=>"false"}, "3"=>{"email"=>"", "_destroy"=>"false"}, "4"=>{"email"=>"", "_destroy"=>"false"}, "5"=>{"email"=>"", "_destroy"=>"false"}, "6"=>{"email"=>"", "_destroy"=>"false"}, "7"=>{"email"=>"", "_destroy"=>"false"}, "8"=>{"email"=>"", "_destroy"=>"false"}, "9"=>{"email"=>"", "_destroy"=>"false"}, "10"=>{"email"=>"", "_destroy"=>"false"}, "11"=>{"email"=>"", "_destroy"=>"false"}, "12"=>{"email"=>"", "_destroy"=>"false"}, "13"=>{"email"=>"", "_destroy"=>"false"}, "14"=>{"email"=>"", "_destroy"=>"false"}, "15"=>{"email"=>"", "_destroy"=>"false"}, "16"=>{"email"=>"", "_destroy"=>"false"}, "17"=>{"email"=>"", "_destroy"=>"false"}, "18"=>{"email"=>"", "_destroy"=>"false"}, "19"=>{"email"=>"", "_destroy"=>"false"}, "20"=>{"email"=>"", "_destroy"=>"false"}, "21"=>{"email"=>"", "_destroy"=>"false"}, "22"=>{"email"=>"", "_destroy"=>"false"}, "23"=>{"email"=>"", "_destroy"=>"false"}, "24"=>{"email"=>"", "_destroy"=>"false"}, "25"=>{"email"=>"", "_destroy"=>"false"}, "26"=>{"email"=>"", "_destroy"=>"false"}, "27"=>{"email"=>"", "_destroy"=>"false"}, "28"=>{"email"=>"", "_destroy"=>"false"}, "29"=>{"email"=>"", "_destroy"=>"false"}, "30"=>{"email"=>"", "_destroy"=>"false"}, "31"=>{"email"=>"", "_destroy"=>"false"}, "32"=>{"email"=>"", "_destroy"=>"false"}, "33"=>{"email"=>"", "_destroy"=>"false"}, "34"=>{"email"=>"", "_destroy"=>"false"}, "35"=>{"email"=>"", "_destroy"=>"false"}, "36"=>{"email"=>"", "_destroy"=>"false"}, "37"=>{"email"=>"", "_destroy"=>"false"}, "38"=>{"email"=>"", "_destroy"=>"false"}, "39"=>{"email"=>"", "_destroy"=>"false"}, "40"=>{"email"=>"", "_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"=>""}}, "commit"=>"Save", "id"=>"6243"}

This shouldnt be. As I show in the controller above, I only build one notification per alert, so why it sends all the notifications back as if they are all associated with the first alert, when I check the first alert only, is beyond me.

update:
even when I used create! instead of build to actually write the associated record, it still presented same problem: unable to get the alert id inside the name attribute of the notification_email input field

Thanks for response

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:54:04+00:00Added an answer on May 31, 2026 at 4:54 pm

    You’re not going to like it. But this is what I did to make it work…

    Information based on this stackoverflow question and was heavily influenced by this pretty awesome answer I can up with this:

    models/alert_rules_set.rb

    # NOTICE: I'm not inheriting from ActiveRecord::Base
    class AlertRulesSet
      extend ActiveModel::Naming
      include ActiveModel::Conversion
    
      attr_accessor :alert_rules
    
      def alert_rules_attributes=(attributes)
        # I'm tricking "fields_for" here.
        # This should never actually be called
        raise
      end
    
      # Strip fields_for indexes and return an array
      def self.alert_rules_from(collection_set_hash)
        collection_set_hash[:alert_rules_attributes].values
      end
    
      def persisted?
        false
      end
    end
    

    Controller

    def alerts_config
      //more code
      @alert_rules_set = AlertRulesSet.new
      @alert_rules_set = AlertRules.all # Or Whatever
      @alert_rules_set.alert_rules.each { |a| a.notification_emails.build }
    
      render :partial => 'home/alerts_config', :layout => false
    end
    
    def update_alerts_config
      @alert_rules = AlertRulesSet.alert_rules_from(params[:alert_rules_set])
    
      # Save Logic here
    end
    

    Form

    = form_for @alert_rules_set, :url => '/home/save_alerts_config/' + @unit.id.to_s, :remote => true, :class => 'ajaxForm' do |f|
      %table.scrollTable{:cellspacing => "0", :width => "740px", :style => "border-collapse: collapse; border-spacing: 0;"}
      %thead.fixedHeader
        %tr
          %th Alerts
          %th Enable
          %th Primary Email
          %th Notification Emails
          %th
      %tbody.scrollContent
        = f.fields_for :alert_rules do |alert_rules_builder|
          %tr
            %td= rule.alert_code.name
            %td= check_box_tag "enabled_ids[]", rule.id
            %td= alert_rules_builder.text_field :email, 
                             :value => rule.email, :index => rule.id
            %td.fields
              = alert_rules_builder.fields_for :notification_emails do |notification_builder|
              = notification_builder.text_field :email
              = notification_builder.hidden_field :_destroy              
              = link_to_function 'Remove Notification',
                    'remove_notifications(this)'.save_panel
    = submit_tag "Save", :class => 'submit myButton'
    

    Here’s a link to a git branch where I got it working with another app and the commit compare.

    This is crazy! What’s going on?

    So historically ActionPack tended to be pretty tightly coupled to ActiveRecord. This isn’t a good idea for several reasons, so starting with Rails 3.0 ActiveModel was introduced. Unfortunately this is one of those cases that could probably be better.

    ActiveModel

    NOTE: Some of this is changing, specifically with this Rails 4 commit

    So basically, I’m implementing the bare minimum to make this case work. Apart from the extend and include directives, the only thing that is necessary to make any other ActiveModel class play nice in this case seems to be persisted?.

    As for everything else…

    attr_accessor :alert_rules
    This is just holding the alert_rules data. This is what fields_for is going to use to populate the fields with existing data.

    alert_rules_attributes=(attributes)
    This is kinda special. This is what fields_for checks for when it wants to know when to render a collection or not. Without this you will just get a single nested field(ie. [alert_rule][notification_emails_attributes]) instead of a collection of nested fields(ie. [alert_rule][notification_emails_attributes][1]).

    self.alert_rules_from(collection_set_hash)
    This is the more confusing part. So basically when you submit some nested attributes they’re actually in a Hash with an index key.

    nested_model_attributes = {
        "0"=>{"attribute"=>"This", "id"=>"0"},
        "1"=>{"attribute"=>"That"}
    }
    

    This method is a quick way to get rid of that and get usable data out (This happens in ActiveRecord too). I figured it would probably be easier to manipulate the data than try to do some saving magic in the AlertRuleSet class (but you could).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have form that checks if a user has unsubmitted changes when they
I have a form that has multiple fields, and for testing purposes is there
We have a form that allows a user to filter a list with by
I am developing a java webapp, using jsp/jquery/ejb/jboss. I have a web-form that enables
I have a form that performs calculations live as the user enters data. However,
I have a windows form application that will open other forms, but will only
I have a form that detects if all the text-fields are valid on each
I'm looking at using ELMAH for the first time but have a requirement that
I have a form submit button that I need to enable the user to
I have form that displays several keywords (standard set of choice lists that changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.