Ruby on Rails中where和find方法有何具体差异?
- 内容介绍
- 文章标签
- 相关推荐
本文共计320个文字,预计阅读时间需要2分钟。
在探讨Difference Between find and Where with Relationships这个问题时,虽然有一些回答,但并不全面。以下是对这一问题的简要
关于`find`与`Where`在处理关系时的区别,已有一些讨论,但内容不够完整。请注意,我在提问时是如何巧妙地改变以吸引注意力的!
接下来是查询的
我执行了查询:`a=Libation.where(user_id=1)`。
这几乎在 Difference Between find and Where with Relationships回答,但并不完全. (请注意我是如何巧妙地改变问题标题的!)我做查询
a = Libation.where("user_id = 1" ) # gets 10 records b = a.sum("count * weight") # Get right answer c = Libation.where("user_id = 2" ) # gets 8 records d = c.sum("count * weight") # Get right answer
现在我做
e = Libation.all # gets 18 records, good f = e.sum("count * weight") # BOOM! I get NoMethodError (undefined method `+' for #<Libation:0x3b91e88>):
坚果.我试图找到相关的文档,但发现很少.或者我找不到合适的地方.
您可以尝试使用#scoped代替:
e = Libation.scoped f = e.sum("count * weight")
本文共计320个文字,预计阅读时间需要2分钟。
在探讨Difference Between find and Where with Relationships这个问题时,虽然有一些回答,但并不全面。以下是对这一问题的简要
关于`find`与`Where`在处理关系时的区别,已有一些讨论,但内容不够完整。请注意,我在提问时是如何巧妙地改变以吸引注意力的!
接下来是查询的
我执行了查询:`a=Libation.where(user_id=1)`。
这几乎在 Difference Between find and Where with Relationships回答,但并不完全. (请注意我是如何巧妙地改变问题标题的!)我做查询
a = Libation.where("user_id = 1" ) # gets 10 records b = a.sum("count * weight") # Get right answer c = Libation.where("user_id = 2" ) # gets 8 records d = c.sum("count * weight") # Get right answer
现在我做
e = Libation.all # gets 18 records, good f = e.sum("count * weight") # BOOM! I get NoMethodError (undefined method `+' for #<Libation:0x3b91e88>):
坚果.我试图找到相关的文档,但发现很少.或者我找不到合适的地方.
您可以尝试使用#scoped代替:
e = Libation.scoped f = e.sum("count * weight")

