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

标签 ruby-on-rails activerecord orm associations rails-activerecord

我想做类似...

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/

相关文章:

php - Codeigniter 中的 result_array() ActiveRecord

java - 用于从 ManyToMany 连接表检索行的 HQL 查询

ruby-on-rails - Rails 4 + Ransack : lteq does not work well with datetime?

mysql - 使用 Codeigniter 从同一个表进行多个 SQL 连接

css - Wicked_pdf 样式表不适用于 Heroku

mysql - 检查模型范围内是否有 locale_restriction

java - HQL:组合左连接和右连接

java - 自定义 hibernate 实体持久性

ruby-on-rails - 如何自动/动态挂载 Rails 引擎?

ruby-on-rails - 如何在 Ruby on Rails 中对按月分组的记录求和