title: How to get a model to use the object to which it belongs in a private method
body: <p>Each User has_many Personas. A User is allowed up to three Personas, and before each Personas is created they are assigned a persona_id of 1, 2 or 3 depending on how many Personas the User already has. The Persona's persona_id is set in a before_validation callback using the set_persona_id private method. My questions is: how do you pass the User to which the Persona belongs to the set_persona_id method? The code below fails. Unit tests reveal that self.user is nil. I tried passing self as an argument to set_persona_id (as in <code>set_persona_id(self)</code> but that also failed, producing a nil user.</p>
  
  <p>Persona.rb:</p>
  
  <pre><code>class Persona &lt; ActiveRecord::Base
  
    belongs_to :user
  
    before_validation do
      set_persona_id
    end
  
    before_save do
      self.callsign.downcase!
    end
  
    validates :name, presence: true,
                     length:   { maximum: 50 }
    VALID_CALLSIGN_REGEX = /\A[a-z\d\-.\_]+\z/i
    validates :callsign, presence:   true,
                         length:     { maximum: 20 },
                         format:     { with: VALID_CALLSIGN_REGEX },
                         uniqueness: { case_sensitive: false }
    validates :persona_id, presence: true
  
    def to_param
      callsign
    end
  
    private
  
      def set_persona_id
        if ( self.user.personas.count == 0 )
          self.persona_id = 1
          return
        end
        if ( self.user.personas.count == 1 )
          if self.user.personas.first.persona_id == 1
            self.persona_id = 2
            return
          else
            self.persona_id = 1
            return
          end
        end
        if ( self.user.personas.count == 2 )
          if self.user.personas.first.persona_id == 1
            if self.user.personas.second.persona_id == 2
              self.persona_id = 3
              return
            else
              self.persona_id = 2
              return
            end
          end
          if self.user.personas.first.persona_id == 2
            if self.user.personas.second.persona_id == 1
              self.persona_id = 3
              return
            else
              self.persona_id = 1
              return
            end
          end
          if self.user.personas.first.persona_id == 3
            if self.user.personas.second.persona_id == 1
              self.persona_id = 2
              return
            else
              self.persona_id = 1
              return
            end
          end
        end
      end # set_persona_id
  
  end # Persona
  </code></pre>
  
  <p>persona_test.rb:</p>
  
  <pre><code>def setup
    @user = User.new(name: "Example User", email: "user@example.com", callsign: "example",
                       password: "foobar", password_confirmation: "foobar")
    @user.save
    @persona = @user.personas.build(name: "Bazman", callsign: "Baz")
    @persona.persona_id = 1
  end
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Personas are created by the user filling in some forms and clicking on a 'Create a new persona' button:</p>
  
  <p>views/personas/new.html.erb:</p>
  
  <pre><code>&lt;% provide(:title, 'Create persona') %&gt;
  &lt;h1&gt;Create a new persona&lt;/h1&gt;
  
  &lt;div class="row"&gt;
    &lt;div class="span6 offset3"&gt;
      &lt;%= form_for(@persona) do |f| %&gt;
        &lt;%= render 'shared/error_messages', object: f.object %&gt;
  
        &lt;%= f.label :name %&gt;
        &lt;%= f.text_field :name %&gt;
  
        &lt;%= f.label :callsign %&gt;
        &lt;%= f.text_field :callsign %&gt;
  
        &lt;%= f.submit "Create a new persona", class: "btn btn-large btn-primary" %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In the Personas controller:</p>
  
  <pre><code>def create
    @persona = current_user.personas.build(persona_params)
    if @persona.save
      flash[:success] = "Welcome, " + @persona.name
      redirect_to @persona
    else
      render 'new'
    end
  end
  </code></pre>