-2

「従業員」が「トランザクション」のおかげで「アイテム」をチェックインおよびチェックアウトできる小さなアプリがあります。それらはプレイ中のモデルです。現在チェックアウトされているアイテムを表示するのに苦労しています。また、スコープについて読んでいる間、関連付け、スコープ、およびビューを機能させるのに苦労しています。私は髪を引っ張っています-そして私はレールに慣れていないので、優しくしてください.

私はこのアプリのパフォーマンスについてあまり心配していません。また、ビュー内でネストされた条件文がひどいものであることも知っています。パーシャルを使用する必要があることもわかっていますが、これは実際には MVP のためだけのものです。

デシベル/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

models/employee.rb

    class Employee < ActiveRecord::Base
      attr_accessible :name, :phone

      has_many :transactions
      has_many :items, :through => :transactions
    end

models/item.rb

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

      has_many :transactions
      has_many :employees, :through => :transactions

      scope :checked_out, -> { last_transaction.checkout }
    end

models/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

      scope :last_transaction, -> { order('created_at DESC').limit(1) }

      scope :checkin, where(:status => true)
      scope :checkout, where(:status => false)
    end

アプリ/ビュー/従業員/show.html.erb

    <% if @employee.items.checked_out %>
      <h3>Currently Checked-OUT Items</h3>
      <table>
        <tr>
          <th>Item ID</th>
          <th style="padding-left:30px">Asset Tag</th>
        </tr>
        <% @employee.transactions.items.checked_out.reverse.each do |transaction| %>
          <tr>
            <td style="padding-left:30px"> 
              <%= transaction.description %>
            </td>
            <td style="padding-left:30px">
              <%= transaction.assettag %>
            </td>
          </tr>
        <% end %>
      </table>
    <% end %>
4

2 に答える 2

0

問題は、従業員のアイテムが必要なことですが、条件を提供するフィールドがアイテムと条件の間のモデルにあります。1 つの方法は、従業員モデルに関連付けを追加することです。

class Employee < AR::Base
  has_many :current_transactions, :conditions => { :status => CHECKED_OUT }
  has_many :checked_out_items, :through => :current_transactions
end
于 2013-06-18T03:21:00.787 に答える
0

last_transaction はトランザクションで定義されているため、メソッドなしエラーが発生します。そのスコープは

     scope :checked_out, -> { transactions.last_transaction.checkout }

すべてのトランザクションが必要な場合は、last_transactions スコープを削除します

     scope :checked_out, -> { transactions.checkout }
于 2013-06-18T03:40:36.537 に答える