ruby - Rails to excel with associations -


i'am new on rails , i've spent day looking solution export data excel file.

i've tryed to_xls, simple_xlxs , other gems, i've tryed render xml template , i've no success.

so have associations between 2 models:

call model:

class call < activerecord::base   belongs_to :number   has_many :results end 

and result model:

class result < activerecord::base   belongs_to :call end 

so need genrate excel tables own headers in table calle'd want. , wan't in excel file columns associeted model

what can do? thanks

# install in gem file or in terminal test # gem install spreadsheet   require 'spreadsheet'  spreadsheet.client_encoding = 'utf-8'  book = spreadsheet::workbook.new sheet1 = book.create_worksheet :name => 'test'  money_format = spreadsheet::format.new :number_format => "#,##0.00 [$€-407]" date_format = spreadsheet::format.new :number_format => 'mm.dd.yyyy'  # set default column formats sheet1.column(1).default_format = money_format sheet1.column(2).default_format = date_format  # depending on data have create loop fits  format of data sheet1.row(0).push "just text", 5.98, datetime.now  book.write 'sample.xls' 

the above code example writes data columns create. can have info in csv style. if you're returning objects can values each object , join array ',' separation , loop through.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -