ruby-on-rails - 在范围内使用关联域进行计算
问题描述
Another
有字段some_field
。
ThisTable
belongs_to
Another
并且有字段local_field
。
现在我想写some_filter
范围ThisTable
:
scope :some_filter, ->(var_in) {
tmp1 = calc_sth(another.some_field, var_in)
tmp2 = calc_sth_else(another.some_field, var_in)
where(local_field: tmp1..tmp2)
}
我有错误undefined local variable or method 'another' for #<ActiveRecord::AssociationRelation []>
这个怎么写?
解决方案
范围不在对象的实例上执行,它们基本上是类方法。通过another
在您的范围内使用,您并没有真正指出ThisTable
您想要获得Another
的内容。就好像你打电话一样ThisTable.another
- 它不起作用。
推荐阅读
- asp.net - 如何将 Vue JS 代码插入 .Net MVC 项目
- wpf - ListView 项目源绑定不使用 mvvm
- python - 访问另一个类 Tkinter 中的属性 - OOP
- excel - VBA:如果在参考表中找到值,则将数据+字段从源表拉到目标表
- r - 如何使用ggplot将图例放在多个分布的峰值中?
- google-app-engine - Google Cloud:您不是此证书的域的授权所有者
- python - 如何为 3.x 到 2.x IPC 设置 Python 多处理 pickle 协议
- unity3d - Waypoint Circuit 在游戏模式下存在问题,但在场景视图中自行修复
- wpf - 有没有办法将多个 EditingElementStyle 属性组合到 1 个 DataGridTextColumn 中?XAML WPF
- java - 如何在 Java 中使用 Elasticsearch 摄取附件处理器插件