给出Bank
has_many
s的模型Account
。用关联的银行调用收款方法时,如何获取银行的ID?或换种说法,与关联方法关联的方法范围内的变量是什么?
例如:
class Bank
def do_something_to_accounts
accounts.do_something
end
end
class Account
def self.do_something
where(name: 'Checking') # this would query against name AND bank_id
create(name: 'Saving') # this would create an account with name AND bank_id from the associated bank
# how to determine the bank_id here?
end
end
我注意到有一个名为scope_attributes
的方法将显示{bank_id: ID}