title: Rails 5 Constant & Model & Virtual Attributes
body: <p>I am trying to use predefined constant in Model virtual attributes.</p>
  
  <p>when I create user, it shows absolute <code>avatar_url</code> and works fine.</p>
  
  <p><strong>Problem:</strong> </p>
  
  <p>when I find user in login method it return only relative <code>url</code> i.e <code>"avatar_url": "/avatars/original/missing.png"</code> which means <code>#{SERVER_BASE_PATH}</code> is not interpolating at that moment.</p>
  
  <p>It also works fine with some <code>api</code> call i.e when updating user. but not in all cases. please help me how can I fix this problem in order to get absolute url in all <code>api</code> calls</p>
  
  <p><strong>Example:</strong></p>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>class User &lt; ApplicationRecord
      # user model
      # if avatar url is empty then use default image url
      # SERVER_BASE_PATH is defined in config/initializer/constant.rb
      attribute :avatar_url, :string, default: -&gt; { "#{SERVER_BASE_PATH}/avatars/original/missing.png" }
  end
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <p>it has simple login method</p>
  
  <pre><code>class UsersController &lt; ApplicationController
      def login
          response = OK()
          unless params[:email].present? and params[:password].present?
              render json: missing_params_specific('either user [email] or [password]') and return
          end
          response[:user] = []
          response[:status] = '0'
  
          begin
          user = User.find_by(email: params[:email].downcase)
          if user &amp;&amp; user.authenticate(params[:password])
              # following line first check if user profile image exist then it places that image url given by paperclip
              # if not exist then url defined in model virtual attributes is used.
              user.avatar_url = user.avatar.url if user.avatar.url.present?
              user.set_last_sign_in_at user.sign_in_count
            response[:user] = user
            response[:status] = '1'
          else
            response[:message] = 'Invalid email/password combination' # Not quite right!
          end
          rescue =&gt; e
              response[:message] = e.message
          end
          render json: response and return
      end
  end
  </code></pre>
  
  <p><strong>API JSON Response:</strong></p>
  
  <pre><code>{
    "JSON_KEY_STATUS_CODE": 1,
    "JSON_KEY_STATUS_MESSAGE": "OK",
    "server_time": 1490623384,
    "user": {
      "confirmed": true,
      "user_status": "Active",
      "admin": false,
      "user_role": "Standard",
      "first_name": "Super",
      "last_name": "User",
      "full_name": "Super User",
      "avatar_url": "/avatars/original/missing.png",  &lt;-- Here (not absolute url)
      "is_active": true,
    },
    "status": "1"
  }
  </code></pre>