This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wb = xlsx_package.workbook | |
wb.add_worksheet(name: "Student") do |sheet| | |
title = wb.styles.add_style(b: true, bg_color: "395870", fg_color: "FFFFFF", | |
alignment: {horizontal: :center}) | |
sheet.add_row ["ID", "Name", "Date of Birth", "Email", | |
"School", "School Address"], style: title | |
@students.each do |student| | |
sheet.add_row [student.id, student.full_name, student.DOB, student.email, | |
student.school_name, student.school_address] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
def index | |
@students = Student.all_with_school_details | |
respond_to do |format| | |
format.html | |
format.csv { send_data @students.as_csv } | |
format.xlsx | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
# Use axlsx to create spreadsheets | |
gem 'rubyzip', '~> 1.1.0' | |
gem 'axlsx', '2.1.0.pre' | |
gem 'axlsx_rails' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
<%= link_to "Export CSV", students_path(format: :csv) %> | |
[...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
@students = Student.all_with_school_details | |
respond_to do |format| | |
format.html | |
format.csv { send_data @students.as_csv } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
students GET /students(.:format) students#index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
def self.as_csv | |
CSV.generate do |csv| | |
columns = %w(id first_name last_name DOB school_name school_address) | |
csv << columns.map(&:humanize) | |
all_with_school_details.each do |student| | |
csv << student.attributes.values_at(*columns) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Student.all_with_school_details.first.school_name | |
=> "Springfield Elementary School" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Student.all_with_school_details.first | |
=> #<Student:0x007f9c26baa580 | |
id: 1, | |
first_name: "Bart", | |
last_name: "Simpson", | |
DOB: Sun, 17 Dec 1989, | |
email: "bart@the-simpsons.com", | |
created_at: Sun, 04 Dec 2016 06:26:52 UTC +00:00, | |
updated_at: Sun, 04 Dec 2016 06:26:52 UTC +00:00, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
class Student < ApplicationRecord | |
belongs_to :school | |
[...] | |
def self.all_with_school_details | |
Student.select("students.*, schools.name as school_name, schools.address as school_address").joins(:school) |
NewerOlder