我用Rails 3,我似乎无法检查一个给定的实例是在一个范围内,在这里看到:

p = Post.find 6
+----+----------+-------------------------+-------------------------+-------------------------+-----------+
| id | title    | publish_date            | created_at              | updated_at              | published |
+----+----------+-------------------------+-------------------------+-------------------------+-----------+
| 6  | asfdfdsa | 2010-03-28 22:33:00 UTC | 2010-03-28 22:33:46 UTC | 2010-03-28 22:33:46 UTC | true      |
+----+----------+-------------------------+-------------------------+-------------------------+-----------+

我有一个menu范围看起来像:

scope :menu, where("published != ?", false).limit(4)

当我运行它,我得到:

Post.menu.all
+----+------------------+------------------+------------------+-------------------+-----------+
| id | title            | publish_date     | created_at       | updated_at        | published |
+----+------------------+------------------+------------------+-------------------+-----------+
| 1  | Lorem ipsum      | 2010-03-23 07... | 2010-03-23 07... | 2010-03-28 21:... | true      |
| 2  | fdasf            | 2010-03-28 21... | 2010-03-28 21... | 2010-03-28 21:... | true      |
| 3  |  Ruby’s Imple... | 2010-03-28 21... | 2010-03-28 21... | 2010-03-28 21:... | true      |
| 4  | dsaD             | 2010-03-28 22... | 2010-03-28 22... | 2010-03-28 22:... | true      |
+----+------------------+------------------+------------------+-------------------+-----------+

这是正确的,但如果我尝试检查,如果p是在该menu范围使用:Post.menu.exists?(p)我得到true当它应该是false

什么是找出如果事情给定的情况下是在一个范围内的正确方法?

有帮助吗?

解决方案

事实上,我能够使用include?代替exists?的该阵列的方法来解决它

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top