【发布时间】:2015-07-08 14:48:21
【问题描述】:
我想在ActiveRecord::Relation 对象中查找特定记录,以便获取该记录的属性。
以下内容有效,但问题是它再次使用 find_by 语句访问数据库。它不应该。 rails 应该有一种方法可以在 ActiveRecord::Relation 对象中找到该对象,而不必再次查询数据库。
#returns an ActiveRecord::Relation object
@blogs = Blog.all
# Search for the blog within that ActiveRecord::Relation object, NOT the database
@blogs.find_by(id: 1).title #do this statement but don't hit the database again
【问题讨论】:
标签: ruby-on-rails ruby activerecord