title: Active Record and file: How do i write Json file with my data?
body: <p>How do I write a data in table event to json file?
  Please see this code:</p>
  
  <p>In model event.rb</p>
  
  <pre><code> class Event &lt; ActiveRecord::Base
    attr_accessible :name, :event_description, :start_at, :end_at, :status, :eventable_id
    has_event_calendar
    belongs_to :eventable, polymorphic: true
    after_save :write_json
  
  
  end
  def write_json
      Event.all.each do |event|
              @eventJson = {
              "id" =&gt; event.id,
              "start" =&gt; event.start_at,
              "end" =&gt; event.end_at,
              "title" =&gt; event.name,
              "body" =&gt; event.event_description,
              "status" =&gt; event.status
              } 
  
      end
      File.open("public/event.json","w") do |f|
        f.write(@eventJson.to_json)
      end 
  
   end
  </code></pre>
  
  <p>In file <code>Json</code> there's one record, but in table <code>event</code> there are many records. How do I write all records from table <code>event</code> to event.json file after saving the record?</p>
  
  <p>public/event.json</p>
  
  <pre><code>{"id":35,"start":"2013-03-28T00:00:00Z","end":"2013-03-28T00:00:00Z","title":"1345edrewrewr","body":"123124","status":"Confirm"}
  </code></pre>