Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4-如何编写作用域_Ruby On Rails_Methods_Scope - Fatal编程技术网

Ruby on rails Rails 4-如何编写作用域

Ruby on rails Rails 4-如何编写作用域,ruby-on-rails,methods,scope,Ruby On Rails,Methods,Scope,我试图找出如何编写一个范围来检查相关属性的存在 我想尝试的要点是我的用户模型中的范围: scope :onboarded, -> { where ("user.profile_id IS NOT NULL AND user.profile.organisation_id IS NOT NULL") } 我有另一个模型叫Profile。这些协会是: User has_one profile Profile belongs to User. 上面的范围写在我的用户模型中。这是基于这篇文章

我试图找出如何编写一个范围来检查相关属性的存在

我想尝试的要点是我的用户模型中的范围:

scope :onboarded, -> { where ("user.profile_id IS NOT NULL AND user.profile.organisation_id IS NOT NULL") } 
我有另一个模型叫Profile。这些协会是:

User has_one profile
Profile belongs to User.
上面的范围写在我的用户模型中。这是基于这篇文章的建议


我不确定我是否应该尝试在我的配置文件模型中创建一个范围来检查是否有id或组织id,然后以某种方式尝试在我的用户模型中测试,或者我是否可以在我的用户范围中将模型链接在一起以测试这一点。

最简单的方法可能是只使用
连接
,因为这将执行内部连接,因为您实际上是在检查现有的关联,而不仅仅是简单的属性

考虑到您在
档案
组织
之间也有
归属关系,您可以这样做:

scope :onboarded, -> { joins(profile: :organization) }

这将返回具有组织的配置文件的所有用户。

最简单的方法可能是只使用
联接,因为这将执行内部联接,因为您实际上是在检查现有关联,而不仅仅是简单属性

考虑到您在
档案
组织
之间也有
归属关系,您可以这样做:

scope :onboarded, -> { joins(profile: :organization) }
这将返回具有组织的配置文件的所有用户