我想做类似的事情...
class Person
has_many :things, -> { where(attr: owner.attr) }
end
即,声明一个具有scope块的has_many关联,在该块内,我需要访问关联所有者,即对人员模型的引用。
因此,如果我最终调用@some_person.things
,则上面作用域块中的owner
将是对@some_person
的引用。
我可以使用什么代替上面代码中的owner
来引用该块中的关联所有者?
这似乎是一种可行的方法。从这里举起:Convert Rails 4 has_many from condition with proc to where
has_many :things, -> (object){ where( attr: object.attr )}