title: after_validation :set_full_name in ruby on rails
body: <p>Problem:</p>
  
  <p>I'm making an aged care app. I have two models <code>Room</code> &amp; <code>Area</code>. I have created a column on the room table called full_name After validation, I want to save the full name This will be used as friendly label display </p>
  
  <p>I'll use this on the resident form</p>
  
  <pre><code>  &lt;div class="col-md-4"&gt;
      &lt;%= f.association :room, collection: @current_business.rooms.order(:name).pluck(:full_name, :id), prompt: "Choose a room"   %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>What I've tried:</p>
  
  <p>I've read many stack articles. I've read ruby docs. I'm only a junior developer and I'm stuck, please help!</p>
  
  <p>I think the problem is in this section:</p>
  
  <pre><code> def set_full_name
      if @room.name.changed? || @area.name_changed?
        self.full_name = [@room.name, @area.name].select(&amp;:present?).join(' ').titleize
      end
    end
  </code></pre>
  
  <p>Room.rb</p>
  
  <pre><code># == Schema Information
  #
  # Table name: rooms
  #
  #  id          :bigint(8)        not null, primary key
  #  name        :string
  #  created_at  :datetime         not null
  #  updated_at  :datetime         not null
  #  area_id     :bigint(8)
  #  business_id :integer
  #  author_id   :integer
  #  deleted_at  :datetime
  #  full_name   :string
  #
  
  class Room &lt; ApplicationRecord
    acts_as_paranoid
  
    after_validation :set_full_name
  
    validates :name, presence: true, length: { maximum: 50 }
    belongs_to :area
    has_many :residents
  
    def set_full_name
      if @room.name.changed? || @area.name_changed?
        self.full_name = [@room.name, @area.name].select(&amp;:present?).join(' ').titleize
      end
    end
  
  end
  </code></pre>
  
  <p>Area.rb</p>
  
  <pre><code># == Schema Information
  #
  # Table name: areas
  #
  #  id          :bigint(8)        not null, primary key
  #  name        :string
  #  created_at  :datetime         not null
  #  updated_at  :datetime         not null
  #  location_id :bigint(8)
  #  business_id :integer
  #  author_id   :integer
  #  deleted_at  :datetime
  #
  
  class Area &lt; ApplicationRecord
    acts_as_paranoid
    belongs_to :location
    belongs_to :business
    validates :name, presence: true, length: { maximum: 100 }
  
    has_many :rooms, -&gt; { order(id: :asc) }, inverse_of: :area
    accepts_nested_attributes_for :rooms, reject_if: :all_blank, allow_destroy: true
  
  
  
  end
  </code></pre>
  
  <p>Rooms_controller</p>
  
  <pre><code>class App::RoomsController &lt; App::BaseController
    before_action :set_room, only: [:show, :edit, :update, :destroy]
  
    # GET /rooms
    # GET /rooms.json
    def index
      @rooms = current_business.rooms.order(name: :asc).paginate(:page =&gt; params[:page])
    end
  
    # GET /rooms/1
    # GET /rooms/1.json
    def show
    end
  
    # GET /rooms/new
    def new
      @room = current_business.rooms.new
    end
  
    # GET /rooms/1/edit
    def edit
    end
  
    # POST /rooms
    # POST /rooms.json
    def create
      @room = current_business.rooms.new(room_params)
      @room.author_id = current_user.id
  
      respond_to do |format|
        if @room.save
          format.html { redirect_to app_room_path(@room), notice: 'Room was successfully created.' }
          format.json { render :show, status: :created, location: @room }
        else
          format.html { render :new }
          format.json { render json: @room.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /rooms/1
    # PATCH/PUT /rooms/1.json
    def update
      @room.author_id = current_user.id
  
      respond_to do |format|
        if @room.update(room_params)
          format.html { redirect_to app_room_path(@room), notice: 'Room was successfully updated.' }
          format.json { render :show, status: :ok, location: @room }
        else
          format.html { render :edit }
          format.json { render json: @room.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /rooms/1
    # DELETE /rooms/1.json
    def destroy
      @room.destroy
      respond_to do |format|
        format.html { redirect_to app_rooms_path, notice: 'Room was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_room
        @room = current_business.rooms.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def room_params
        params.require(:room).permit(:name, :area_id, :deleted_at, :business_id, :author_id, :business_id, :author_id, :full_name)
      end
  end
  </code></pre>