0

Rails3モデルに簡単な質問があります。私が持っているモデルは次のとおりです。

  class Order < ActiveRecord::Base
      attr_accessible :customer :date #blahblah..
      has_many :items
      accepts_nested_attributes_for :items



  class Item < ActiveRecord::Base
      belongs_to :order

@item.customerでは、どうすれば自分のプログラムに到達できますか?

ありがとう

4

1 に答える 1

1

order協会自体を通じて。

@item.order.customer

customerオブジェクトから直接アクセスする便利なメソッドが必要な場合は、Itemいくつかのカスタム アクセサー メソッドを作成する必要があります。

class Item < ActiveRecord::Base

  ...

  def customer
    self.order.customer
  end

  def customer=(new_customer)
    self.order.customer = (new_customer)
  end
end
于 2012-12-14T04:39:41.827 に答える