ActiveRecord关联范围块内的访问所有者

问题描述 投票:1回答:1

我想做类似的事情...

class Person
  has_many :things, -> { where(attr: owner.attr) }
end

即,声明一个具有scope块的has_many关联,在该块内,我需要访问关联所有者,即对人员模型的引用。

因此,如果我最终调用@some_person.things,则上面作用域块中的owner将是对@some_person的引用。

我可以使用什么代替上面代码中的owner来引用该块中的关联所有者?

ruby-on-rails activerecord orm associations rails-activerecord
1个回答
6
投票

这似乎是一种可行的方法。从这里举起:Convert Rails 4 has_many from condition with proc to where

has_many :things, -> (object){ where( attr: object.attr )}
© www.soinside.com 2019 - 2024. All rights reserved.