作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
def maturities
InfoItem.find_all_by_work_order(self.work_order).map(&:maturity)
end
def maturities
InfoItem.where(work_order: self.work_order).map(&:maturity)
end
.where
比
find_all_by
更常见如今。
最佳答案
我的观点是使用 .where
是一种更好的方法。
当您使用基于属性的查找器时,您将不得不通过一个方法丢失调用并最终定义一个类方法,通过 class_eval
,返回你的结果。这是您可能不需要做的额外处理。
此外,串在一起:find_by_this_and_this_and_this_and_this... 会变得丑陋。
See how rails accomplishes attribute based finders here
github上的模块DynamicMatchers缺少方法:
def method_missing(name, *arguments, &block)
match = Method.match(self, name)
if match && match.valid?
match.define
send(name, *arguments, &block)
else
super
end
end
关于ruby-on-rails - rails "find_all_by"与 ".where",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232971/
我有以下代码: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end 我正在考虑将
在查看 haml 文件中我有一个链接,当您单击时,您会看到所有按标题或发布日期排序的电影。 Controller 中的代码是: def index sort = params[:sort] |
我是一名优秀的程序员,十分优秀!