trying to create an "award" record. I have 2 selection boxes calling the Employee model to populate the Nominator and Nominee in the Award.
The Employee table gets dropped and recreated every night via a batch job, so I need to capture all the metadata attributes (first_name, last_name, etc) of the Nominator and Nominee at the time of award creation.
Normally, I would just have an employee_id foreign key in the award model, then call the employee attributes dynamically, but the employee data is dropped each night, so I'm totally stuck on the logic on how to do this. Please help! And thanks in advance!
class AwardsController < ApplicationController
def create
@award = Award.new(params[:award])
respond_to do |format|
if @award.save
format.html { redirect_to new_award_path, notice: 'Award was successfully created.' }
format.json { render json: @award, status: :created, location: @award }
else
format.html { render action: "new" }
format.json { render json: @award.errors, status: :unprocessable_entity }
end
end
end
end
-
<%= form_for(@award) do |f| %>
<% if @award.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@award.errors.count, "error") %> prohibited this award from being saved:</h2>
<ul>
<% @award.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h4>I am...</h4>
<label>Name</label>
<%= f.collection_select :nominator_username, Employee.active.order(:last_name), :username, :lastfirst, :include_blank => true %>
<hr>
<h4>I would like to nominate...</h4>
<label>Name</label>
<%= f.collection_select :nominee_username, Employee.active.order(:last_name), :username, :lastfirst, :include_blank => true %>
<h4>For providing the following...</h4>
<%= f.text_area :award_description, :size => "50x7" %>
<button type="submit">Submit</button>
<div class="spacer"></div><p>
<% end %>
-
class CreateAwards < ActiveRecord::Migration
def change
create_table :awards do |t|
t.string :nominator_first_name
t.string :nominator_last_name
t.string :nominator_username
t.string :nominator_phone
t.string :nominator_employee_number
t.string :nominator_mail_station
t.string :nominee_first_name
t.string :nominee_last_name
t.string :nominee_username
t.string :nominee_employee_number
t.string :nominee_phone
t.string :nominee_mail_station
t.text :award_description
t.timestamps
end
end
end
UPDATE:
Okay, I created a rake task:
#\lib\tasks\import.rake
require 'csv'
desc "Import employees from csv file"
task :import => [:environment] do
file = "db/users.csv"
CSV.foreach(file, :headers => true) do |row|
Employee.find_or_create_by_username({
:username => row[0],
:last_name => row[1],
:first_name => row[2],
:employee_number => row[3],
:phone => row[4],
:mail_station => row[5]
}
)
end
end
When I run rake import.rake, I get:
*Don't know how to build task 'import.rake'*
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/task_manager.rb:49:in `[]'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:142:in `invoke_task'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/.rvm/gems/ruby-1.9.3-p286/gems/rake-10.0.4/bin/rake:33:in `<top (required)>'
/.rvm/gems/ruby-1.9.3-p286/bin/rake:23:in `load'
/.rvm/gems/ruby-1.9.3-p286/bin/rake:23:in `<main>'
UPDATE - Here is my final rake task. I am getting an error with the final update_column block: *undefined method 'update_column' for nil:NilClass*
require 'csv'
desc "Import employees from csv file"
task :import => [:environment] do
file = "db/users1.csv"
to_be_deactivated = {}
Employee.all.each do |e|
to_be_deactivated[ e.employee_number ] = true
end
CSV.foreach(file, headers: true) do |row|
e = Employee.find_or_create_by_employee_number( row[:employee_number] )
to_be_deactivated[ e.employee_number ] = false
e.update_attributes(
:last_name => row[1],
:first_name => row[2],
:phone => row[4],
:mail_station => row[5])
e.save
end
to_be_deactivated.each do |n|
e = Employee.find_by_employee_number( n )
e.update_column(:active, false)
end
end