【发布时间】:2013-08-03 16:33:22
【问题描述】:
我正在使用 rails v3.0.9
我不知道如何在活动记录集合中使用 find 方法
我在控制台中尝试过的,
@customers = Customer.all # returns collection of records
@customer = @customers.find {|customer| customer.id == 1 } # returns the correct record
现在我在关联集合中使用相同的查找方法
@projects = @customer.projects # returns collection of Project records
@project = @projects.find {|project| project.id == 1 } # got error
ActiveRecord::RecordNotFound: Couldn't find Project with ID=1...
请任何人解释 find 方法与上述两个示例有何不同
有没有其他方法可以在关联集合中查找记录?
我使用数组检测来获取我的项目记录
@project = @projects.detect {|project| project.id == 1 } # returns the correct record
哪种方法最好从活动记录数组中找到单个记录?
【问题讨论】:
标签: ruby-on-rails-3 activerecord associations