「従業員」が「トランザクション」のおかげで「アイテム」をチェックインおよびチェックアウトできる小さなアプリがあります。それらはプレイ中のモデルです。現在チェックアウトされているアイテムを表示するのに苦労しています。また、スコープについて読んでいる間、関連付け、スコープ、およびビューを機能させるのに苦労しています。私は髪を引っ張っています-そして私はレールに慣れていないので、優しくしてください.
私はこのアプリのパフォーマンスについてあまり心配していません。また、ビュー内でネストされた条件文がひどいものであることも知っています。パーシャルを使用する必要があることもわかっていますが、これは実際には 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 %>