1

「受賞」レコードを作成しようとしています。従業員モデルを呼び出す 2 つの選択ボックスがあり、賞の指名者と被指名者を入力します。

従業員テーブルは、バッチ ジョブを介して毎晩ドロップされ、再作成されるため、賞の作成時に推薦者と推薦者のすべてのメタデータ属性 (first_name、last_name など) を取得する必要があります。

通常、私は賞モデルに employee_id 外部キーを用意し、従業員属性を動的に呼び出しますが、従業員データは毎晩ドロップされるため、これを行う方法のロジックに完全に行き詰まっています。助けてください!そして、事前に感謝します!

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

アップデート:

さて、レーキ タスクを作成しました。

#\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

rake import.rake を実行すると、次のようになります。

*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_column ブロックでエラーが発生します: *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
4

1 に答える 1