我想做类似...
class Person
has_many :things, -> { where(attr: owner.attr) }
end
也就是说,用范围 block 声明一个 has_many 关联,在该 block 内我需要访问关联所有者,即对人员模型的引用。
因此,如果我最终调用 @some_person.things
,上面范围 block 中的 owner
将是对 @some_person
的引用。
在上面的代码中,我可以使用什么来代替 owner
来引用该 block 内的关联所有者?
最佳答案
这似乎是一种可行的方法。从这里解除:Convert Rails 4 has_many from condition with proc to where
has_many :things, -> (object){ where( attr: object.attr )}
关于ruby-on-rails - 在 ActiveRecord 关联范围 block 内访问所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358824/