これが私の協会です...
Account has_many :credits
Credit belongs_to :account
そして私は実行しようとしています:account.credits.current
したがって、その場合、私はすでにオブジェクトを持っているので、モデル内のメソッドAccount
にアクセスしたいと思います。current
Credit
これがその方法です...
def self.current
# Find current credit line
current = self.where(:for_date => Time.now.strftime("%Y-%m-01")).first
# If we couldn't find a credit line for this month, create one
current = Credit.create(:account_id => self.account.id, :for_date => Time.now.strftime("%Y-%m-01")) if current.blank?
# Return the object
current
end
問題はその2行目にあります...クレジットエントリが見つからない場合は、新しいクレジットエントリを作成する必要があります。具体的には、どのアカウントに関連付けるかを設定できません。エラーが発生しますundefined method 'account'
。