How to write data in a table event to a json file? See this code:
In the event.rb model
class Event < 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" => event.id, "start" => event.start_at, "end" => event.end_at, "title" => event.name, "body" => event.event_description, "status" => event.status } end File.open("public/event.json","w") do |f| f.write(@eventJson.to_json) end end
There is one entry in the Json file, but there are many entries in the event table. How to write all the records from the event table to the event.json file after saving the record?
open / event.json
{"id":35,"start":"2013-03-28T00:00:00Z","end":"2013-03-28T00:00:00Z","title":"1345edrewrewr","body":"123124","status":"Confirm"}
source share