Search code examples
ruby-on-railsemailtriggersmailer

Trigger email send when a rails model boolean field is set to true instead of false


I have two models in my application. Families has_many Individuals's. individuals's belongs_to Families.

I'm trying to make it so that when a boolean field in the Families model is set to "true" an email is sent to all of the email addresses stored against Individuals when the family is "updated".

Can anyone help with this? I've been able to get ActiveMailer sending an email when an Individual is created easily enough, but cannot find a way to trigger on the update of a specific field?

Any help would be greatly appreciated!


Solution

  • Maybe you could use an after filter?

    on the Family model

    after_update :email_individuals
    
    
    private
    def email_individuals
      if boolean_field_changed?
        individuals.each do |i|
          // send out emails here
        end
      end
    end
    

    The next step would be to move this out of the request cycle and into a queue.

    Hope I grokked your question properly.