title: How to modify to_json method and add a dynamic property to it - Rails 4
body: <p>so i have this controller and i want to add a dynamic attribute along with the other data in the @events instance variable</p>
  
  <p>i have search and tried things like <strong>@events.attributes.merge(appointment: true)</strong></p>
  
  <p><strong>appointment = true</strong> is what i want to add to the events object.</p>
  
  <pre><code>def find
  params = event_params
  
  current_user = 2
  @events = Event.where('date LIKE ?',"%#{params[:month]}%")
  
  def @events.as_json(options = { })
      h = super(options)
      h[:appointments]   = false # Or combine with above h[:appointments] = self.appointments?
      h
  end
  
  respond_to do |format|
    if current_user == 1
  
      if @events
  
        format.json {
          render json: @events.to_json
        }
      else
        render 'index'
      end
    else
  
      format.json {
        render json: @events.to_json
      }
    end
  end
  </code></pre>
  
  <p>end</p>
  
  <p><strong>ajax code here</strong></p>
  
  <pre><code>function retrieve(date_partial) {
  
      var jsondata = {
        events: {
          month: date_partial,
        }
      }
  
      $.ajax({
        cache: false,
        type: "POST",
        url: "/events/find",
        data: jsondata,
        success: function(data) {
          console.log(data);
          for (var i = 0; i &lt; data.length; i++) {
            var day = data[i].date.substring(0, 2);
            $("td[data-day='" + day + "']").addClass('added');
          }
  
        },
        error: function(xhr) {
          alert("The error code is: " + xhr.statusText);
        }
      });
  </code></pre>
  
  <p>so how can i add that property?</p>