0

名前付きスコープを作成しました:

class Activity
  scope :active, where("active = ?", true)

  has_many :attachments, :as => :attachable 
  accepts_nested_attributes_for :attachments
end

a = Activity.active正常に動作しますが、関連付けa.attachmentsはできません (NoMethodError: undefined method `attachments')

4

1 に答える 1

0

そのようなスコープを参照するとActiveRecord::Relation、モデル インスタンスではなくオブジェクトが返されます。そのため、Rails は関連付けメソッドがないと不平を言っています。たとえば、次のようなことを行うことができます

a.first.attachments

また

att = a.includes(:attachments)

次に、このコレクション内のアクティビティに属する添付ファイルにアクセスします

att[0].attachments
于 2012-07-26T07:41:01.127 に答える