title: Undefined method include? for Nil:NilClass
body: <p>Thanks for answering my previous question, but I ran into a new problem.</p>
  
  <p>I'm creating a custom validator that validates whether a user typed in a clean word. This
  is used on my UsersController as a validation method.</p>
  
  <p>I am using the Obscenity gem but created some of my own methods to ensure quality data.</p>
  
  <p><strong>Error Message</strong></p>
  
  <pre><code>NoMethodError: Undefined method include? for Nil:NilClass
  </code></pre>
  
  <p>The problem with this is that my methods work if a record already exists, but they don't work during record creation. I've tried to combat this problem by using</p>
  
  <pre><code>:on =&gt; [:create, :update]
  </code></pre>
  
  <p>but I still receive the same error.</p>
  
  <p><strong>Validation Methods</strong></p>
  
  <pre><code>class MyValidator &lt; ActiveModel::Validator
  
    def mystery_setup
      @mystery_words = # This is a mystery, I can't tell you.
      @mystery_c = @mystery_words.map(&amp;:capitalize)
      @mystery_u = @mystery_words.map(&amp;:upcase)
      @mysteries = @mystery_words + @mystery_c + @mystery_u
      @new_mysteries = @mysteries.map{|mystery|mystery.tr("A-Za-z", "N-ZA-Mn-za-m")}
   end
  
    def validate (user)
    mystery_setup
      if Obscenity.profane?(user.name) \ 
      || @new_mysteries.any?{|mystery|user.name.include?(mystery)} \
      || @new_mysteries.any?{|mystery|user.email.include?(mystery)} \
      || @new_mysteries.any?{|mystery|user.password.include?(mystery)}
        user.errors[:name] &lt;&lt; 'Error: Please select a different username'
      end
    end
  end
  </code></pre>
  
  <p><strong>User.rb(Model)</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    include ActiveModel::Validations
    validates_with MyValidator
  
    has_many :favorites, foreign_key: "user_id", dependent: :destroy
    has_many :pictures, through: :favorites
  
    has_secure_password
    before_create :create_remember_token
  
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  
    validates_presence_of :name, :password, :email
    validates_uniqueness_of :name, :email
    validates :name, length: { in: 3..20 }
    validates :password, length: { minimum: 6 }
    validates :email, format: { with: VALID_EMAIL_REGEX }, length: { in: 8..50 }
  
    validates_confirmation_of :password, if: lambda { |m| m.password.present? }
  
    def User.new_remember_token
      SecureRandom.urlsafe_base64
    end
  
    def User.digest(token)
      Digest::SHA1.hexdigest(token.to_s)
    end
  
    private
  
    def create_remember_token
      self.remember_token = User.digest(User.new_remember_token)
    end
  
  end
  </code></pre>
  
  <p>I have also tried using an unless statement</p>
  
  <pre><code>def validate (user)
    mystery_setup
    unless User.all.include?(user)
      if (Obscenity.profane?(user.name) 
      || @new_mysteries.any {|mystery|user.name.include?(mystery)})  \
      || @new_mysteries.any?{|mystery|user.email.include?(mystery)} \
      ||   @new_mysteries.any?{|mystery|user.password.include?(mystery)}
          user.errors[:name] &lt;&lt; 'Error: Please select a different username'
        end
      end
    end
  end
  </code></pre>
  
  <p>I tried testing if there was a user by using the unless statement but that didn't work either.</p>
  
  <p>Following advice from a similar question here, I changed my migrations file to combat this area.</p>
  
  <pre><code>class CreateUsers &lt; ActiveRecord::Migration
    def change
      create_table :users do |t|
        t.string :name, default: 'new'
        t.string :password, default: 'new'
        t.string :email, default: 'new'
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p><strong>Question Link</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/18063331/undefined-method-include-for-nilnilclass-with-partial-validation-of-wizard-g">undefined method `include?&#39; for nil:NilClass with partial validation of wizard gem</a></p>
  
  <p><strong>Reference for Code</strong></p>
  
  <p><a href="http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations" rel="nofollow noreferrer">http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations</a></p>
  
  <p>Changing the migration file by changing the default values didn't solve this question so I decided to ask a new question here.</p>
  
  <p>This method works for updating records but not for creating new records.</p>
  
  <p>Help is appreciated. Thank you in advanced.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Just received an excellent suggestion to pass in the attributes in bracket format. My code now looks like this</p>
  
  <pre><code>def validate (user)
      mystery_setup
      unless User.all.include?(user)
        if (Obscenity.profane?(user[:name]) || 
          @new_mysteries.any?{|mystery|user[:name].include?(mystery)})  \
        ||@new_mysteries.any?{|mystery|user[:email].include?(mystery)}
        ||@new_mysteries.any?{|mystery|user[:password].include?(mystery)}
          user.errors[:name] &lt;&lt; 'Error: Please select a different username'
        end
      end
  end
  </code></pre>
  
  <p>Right now, it only has an error with the email and password attributes. If I delete the last two ||@new_mysteries.any? lines, my method works for filtering the name.</p>
  
  <p>I would like to keep this professional though, so I'd like to get it to work with the other two methods. Possibly has to do with my use of parentheses or the || symbol?</p>
  
  <p>Solid progress guys, keep it up.</p>
  
  <p>Edit</p>
  
  <p>Also, if I would like to call these validation methods on other classes, would it be better to put this in a helper file?</p>
  
  <p><strong>New Update</strong></p>
  
  <p>Here is my Users Controller code</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    before_action :signed_in_user, only: [:edit, :update, :destroy]
    before_action :correct_user, only: [:edit, :update]
  
    def index
      @users = User.all
    end
  
    def show
      @user = User.find(params[:id])
    end
  
    def new
      @user = User.new
    end
  
    def create
      @user = User.new(user_params)
      if @user.save
        flash[:success] = "Congratulations #{@user.name}! You have successfully created an account"
        redirect_to games_path
      else
        render 'new'
      end
    end
  
    def edit
    end
  
    def update
      @user = User.find(params[:id])
    end
  
    def favorites
      @user = User.find(current_user)
    end
  
    def destroy
    end
  
    private
  
      def user_params
       params.require(:user).permit(:name, :email, :password, :password_confirmation)
     end
  
     def signed_in_user
       unless signed_in?
         store_location
         redirect_to signin_url notice: "Please sign in."
       end
     end
  
     def correct_user
       @user = User.find(params[:id])
       redirect_to(root_url) unless current_user?(@user)
     end
  end
  </code></pre>