集合方法中的Rails关联模型ID

时间:2019-08-07 04:19:09

标签: ruby-on-rails

给出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}

0 个答案:

没有答案