Ruby on rails Rails中视图的作用域和过滤器

Ruby on rails Rails中视图的作用域和过滤器,ruby-on-rails,ruby,erb,Ruby On Rails,Ruby,Erb,我有一个小应用程序,可以让“员工”通过“交易”签入和签出“项目”。这些是正在发挥作用的模式。我很难显示当前已签出的项—虽然我已经阅读了有关作用域的内容,但我很难让关联、作用域和视图正常工作。我正在拔头发——我是rails的新手,所以请温柔一点 我不太担心这个应用程序的性能,我知道视图中嵌套的条件是一件可怕的事情,我知道我应该使用partials,但这实际上只是为了MVP db/schema.rb ActiveRecord::Schema.define(:version => 201

我有一个小应用程序,可以让“员工”通过“交易”签入和签出“项目”。这些是正在发挥作用的模式。我很难显示当前已签出的项—虽然我已经阅读了有关作用域的内容,但我很难让关联、作用域和视图正常工作。我正在拔头发——我是rails的新手,所以请温柔一点

我不太担心这个应用程序的性能,我知道视图中嵌套的条件是一件可怕的事情,我知道我应该使用partials,但这实际上只是为了MVP

db/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
模型/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
app/views/employees/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 %>

问题是您需要员工的项目,但提供条件的字段位于项目和条件之间的模型上。一种方法是向员工模型中添加更多关联

class Employee < AR::Base
  has_many :current_transactions, :conditions => { :status => CHECKED_OUT }
  has_many :checked_out_items, :through => :current_transactions
end

最后一个_事务是在事务上定义的,这就是为什么会出现无方法错误。这一范围应当扩大

     scope :checked_out, -> { transactions.last_transaction.checkout }
如果需要所有事务,请删除最后一个\u事务范围

     scope :checked_out, -> { transactions.checkout }

您在if语句中没有做到这一点吗?我在尝试,但我得到的是:未定义的局部变量或方法“last_transaction”,是因为last_transaction是在事务模型中定义的范围,而不是项吗?如何在项目模型中获取最后一个\u事务作用域?等等,该作用域是在项目上还是在事务上?我只是想确定我明白。这些应该在项目上,你参考范围内的交易