You should call fetch_excel_data from your index action before you are querying the applications. Also, you are not saving the new records, Application.new needs to be replaced with Application.create for example
before_filter :fetch_excel_data
def index
@applicants = Applicant.paginate(page: params[:page])
end
private
def fetch_excel_data
ex = Roo::Excelx.new("data.xlsx")
ex.default_sheet = ex.sheets.first
2.upto(6) do |line|
first_name = ex.cell(line, 'B')
last_name = ex.cell(line, 'C')
Applicant.create(first_name: first_name,
last_name: last_name)
end
end
Since you are saving the imported records into the DB, the import part could be moved out into a rake task or into a separate controller / action so you could easily rerun. It does not need to be run each time you visit the page, I assume.