这里是我的组织...问题与联创
Account has_many :credits
Credit belongs_to :account
而且我试图运行:account.credits.current
所以,在这种情况下,我已经有一个Account
对象,那么我想访问Credit
模型中的current
方法。
这里是一个方法...
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
的问题是,第二行...这应该创建一个新的信贷项,如果它不能找到一个的一个。具体来说,我无法设置应该关联的帐户。我刚刚得到一个undefined method 'account'
错误。
可能是一个长镜头,但你有没有尝试过只是'account.id'? –
@ Zephph:是的......我得到了一个未定义的局部变量或方法'账户'。 – Shpigford
免责声明:这些自我vs平均定义让我感动,我仍然在学习,但如果您将方法从自我中移除,那么该怎么办?本地正常工作:“def current account.id end”Credit.current返回关联账户的ID。 –