Rails模型所有权,归属,委派

时间:2013-05-23 00:12:20

标签: ruby-on-rails ruby

所以,我比上次更进一步。迁移到数据库但我仍然遇到问题。

我正在构建一个通过设计拥有一个“主用户”的应用。该主用户创建我们正在跟踪的员工和项目的记录。然后,我希望能够创建“员工”和“项目”的“交易记录”。因此,当我查看交易时,我会看到它是谁,它是什么项目,我可以查看员工的视图并查看所有关联的交易,以及在查看项目时查看项目的所有关联交易图。

我有如下所示构建的数据库,但我收到的错误如下:

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和belongs_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 %>

1 个答案:

答案 0 :(得分:0)

我将assettag设为12345,但item_id远低于此值。让他们困惑。谢谢CORTEX!