gpt4 book ai didi

ruby-on-rails - 在 ActiveRecord 关联范围 block 内访问所有者

转载 作者:行者123 更新时间:2023-12-01 22:37:05 25 4
gpt4 key购买 nike

我想做类似...

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com