0

I'm working on creating a basic survey app to better learn Rails.

Right now I'm working on learning how to send emails. I've gotten it to work, but now I'm running into a scenario where I send out a lot of duplicate emails. The duplicates are coming out because I'm only checking for a status of Submitted on PUT and then sending out the email.

def update
    p params
    # unless signed_in?
    #   redirect_to signin_path
    # end
    respond_to do |format|
      if @survey.update(survey_params)
        @survey.check_email
        if @survey.status == "Submitted"
          SurveyMailer.survey_created(@survey).deliver
        end
        format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

What's the best way to check/track which emails for a survey I've sent it out to? Basically some sort of "if user has not been notified that a survey has been shared with them, send it out, otherwise don't"

Tom Hammond
  • 5,842
  • 12
  • 52
  • 95
  • What does the "submitted" status actually mean? You might be able to just refine your state model so that it makes finer distinctions between various states, or you can use a mapping table to save a relationship between a user and a specific survey that indicates whether that user has already received a notification e-mail. – MarsAtomic Jun 12 '15 at 01:57
  • Status is more for what the survey looks like in the Backbone app. I don't actually have a good definition of the "submitted" status though :). Draft is for editing the survey, and "In Progress" is to display the graphs showing choice breakdowns. Technically you should be able to share a survey at any state though – Tom Hammond Jun 12 '15 at 01:59
  • The mapping table sounds like a good plan though - would you be able to point me to some documentation on that? I haven't set one of those up before. – Tom Hammond Jun 12 '15 at 01:59
  • You want a [has_and_belongs_to_many](http://guides.rubyonrails.org/association_basics.html) relationship, presumably between user and survey (just guessing since I don't have your design doc sitting in front of me right now). – MarsAtomic Jun 12 '15 at 02:07
  • That looks great - feel free to add that as an answer and I'll accept! – Tom Hammond Jun 12 '15 at 02:08
  • Thanks, but no need. Your question is actually technically a duplicate of http://stackoverflow.com/questions/19473044/rails-4-many-to-many-association-not-working (and probably many others). – MarsAtomic Jun 12 '15 at 02:11

0 Answers0