0

だから、私は前回よりも遠くにいます。データベースに移行しましたが、まだ問題があります。

デバイスを介して「マスターユーザー」が1人いるアプリを構築しています。このマスター ユーザーは、追跡している従業員とアイテムのレコードを作成します。次に、「従業員」と「アイテム」の「取引記録」を作成できるようにしたいと考えています。そのため、トランザクションを見ると、それが誰で、どのアイテムであったかがわかります。また、従業員のビューを見て、関連するすべてのトランザクションを見ることができます。また、アイテムを見るときに、アイテムに関連するすべてのトランザクションを見ることができます見る。

以下に示すようにデータベースを構築しましたが、次のようなエラーが発生します。

Transaction#description delegated to item.description, but item is nil: #<Transaction     id: 4, status: true, item_id: 12345, employee_id: 1, created_at: "2013-05-22 23:49:09", updated_at: "2013-05-22 23:49:09">

各トランザクション レコードに関連付けられているアイテムと従業員に関するすべての情報を表示しようとしています。何が間違っているのかわかりませんが、委譲、has_many、begs_to で十分だと思いました。私は何を間違っていますか?ほんとうにありがとう。

デシベル/schema.rb

    ActiveRecord::Schema.define(:version => 20130516162824) do

      create_table "employees", :force => true do |t|
        t.string   "phone"
        t.string   "name"
        t.datetime "created_at", :null => false
        t.datetime "updated_at", :null => false
      end

      create_table "items", :force => true do |t|
        t.string   "description"
        t.string   "assettag"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

      create_table "transactions", :force => true do |t|
        t.boolean  "status"
        t.integer  "item_id"
        t.integer  "employee_id"
        t.datetime "created_at",  :null => false
        t.datetime "updated_at",  :null => false
      end

      add_index "transactions", ["employee_id"], :name => "index_transactions_on_employee_id"
      add_index "transactions", ["item_id"], :name => "index_transactions_on_item_id"

      create_table "users", :force => true do |t|
        t.string   "email",                  :default => "", :null => false
        t.string   "encrypted_password",     :default => "", :null => false
        t.string   "reset_password_token"
        t.datetime "reset_password_sent_at"
        t.datetime "remember_created_at"
        t.integer  "sign_in_count",          :default => 0
        t.datetime "current_sign_in_at"
        t.datetime "last_sign_in_at"
        t.string   "current_sign_in_ip"
        t.string   "last_sign_in_ip"
        t.datetime "created_at",                             :null => false
        t.datetime "updated_at",                             :null => false
      end

      add_index "users", ["email"], :name => "index_users_on_email", :unique => true
      add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

    end

アプリ/モデル/transaction.rb

    class Transaction < ActiveRecord::Base
      attr_accessible :employee_id, :item_id, :status

      belongs_to :employee
      belongs_to :item

      delegate :phone, :name, to: :employee
      delegate :description, :assettag, to: :item
    end

アプリ/モデル/item.rb

    class Item < ActiveRecord::Base
      attr_accessible :assettag, :description

      has_many :transactions
    end

アプリ/モデル/employee.rb

class Employee < ActiveRecord::Base
  attr_accessible :name, :phone
  has_many :transactions
end

アプリ/ビュー/トランザクション/show.html.erb

    <p id="notice"><%= notice %></p>

    <p>
      <b>Status:</b>
      <%= @transaction.status %>
    </p>

    <p>
      <b>Item </b>
      <%= @transaction.item_id %>
    </p>

    <p>                                                                             
      <b>Item </b>                                                                  
      <%= @transaction.description %>                                                   
    </p>

    <p>
      <b>Employee</b>
      <%= @transaction.name %>
      </p>

    <%= link_to 'Edit', edit_transaction_path(@transaction) %> |
    <%= link_to 'Back', transactions_path %>
4

1 に答える 1

0

assettag は 12345 でしたが、item_id はそれよりもはるかに低くなっています。彼らを混乱させました。CORTEXさんありがとうございます!

于 2013-05-23T00:25:18.820 に答える