Rails has_one 方法的 scope 是否不接受其他参数

hastings · June 15, 2015 · Last by davidwei replied at June 16, 2015 · 2023 hits
class Staff < ActiveRecord::Base
  has_one :last_report, ->(a_s){ p "1111"*90; p a_s;  where(report_type: Report::report_types[a_s]) }, class_name: 'Report'
end

打印 a_s 的时候出现的都是 staff 实例对象本身,查过 ruby on rails 的官方文档,其中有段代码为

has_one :dob, ->(dob) { where("Date.new(2000, 01, 01) > ?", dob) }

这个参数看来不像是实例对象,那推论 has_one 的 scope 应该是能接受其它参数的,可是为啥我的不行

1 Floor has deleted

为毛没人回。。。

3 Floor has deleted

参数是实例本身,如果你要用 scope 可以尝试在 dob 对象上添加 scope 或者在 Staff 里面添加 scope。

另外你举得这个例子,刚才翻了下 guides 没看到你那段代码。

刚才看了下 rails 的代码,官方 master 分支是有的,如果你的不支持,请检查 rails 版本。或者你自己到你本地的 activerecord 下面看下代码。就了然了。我的是 4.1.2,没有此方法。

You need to Sign in before reply, if you don't have an account, please Sign up first.