1

I am using ruby 1.9.2 and Rails 3.0.7. I have written ruby back-end script that will create one CSV file on every 15 min interval using cron job.

Back-end ruby script:

 CSV.open("count.csv", 'wb',:col_sep=>',') do |csv|
 # header row
   csv << ['id', 'count']
   models = Model.all
   models.each do |obj|
      csv << [ obj.id, obj.get_count]
   end
 end

From above script CSV file(count.csv) created successfully. In Rails app,

 CSV.foreach("count.csv", :quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true) do |row|
   count =  row["count"].to_i if row["id"].to_i == @id
 end 

I need to parse count value from that CSV file. but problem is when the time of cron execution, I unable to get count value from that CSV file return zero for all record and after execution finish I can get value of count. But I need count value always whether the cron execution stop or start, Can any one help me to resolve or any suggestion ? Thanks in advance.

9
  • Your code looks fine to me. Are you sure @id is initialized? Commented Nov 15, 2011 at 10:43
  • @daniel - Yes i have initialized. The problem occur during the time of ruby script execution. In Rails reporting side I got zero value for all data. I don't how its happen? any guess? Commented Nov 15, 2011 at 10:50
  • Why are you doing all this as opposed to simply calling obj.get_count in Rails? Commented Nov 15, 2011 at 12:06
  • Have you checked the permissions on count.csv? How about the location? Can you open and read it using File.open? Commented Nov 15, 2011 at 17:19
  • 1
    Not on Heroku you don't. Why bother with a CSV at all? Why not just add an extra table to your database? Commented Nov 17, 2011 at 7:18

1 Answer 1

1
models = Model.all
models.each do |obj|
  csv_string << [ obj.id, obj.get_count]
end

CSV.open("count.csv", 'wb',:col_sep=>',') do |csv|
# header row
 csv << ['id', 'count']
 csv << csv_string
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.