开发者

Rails, if instance is in a scope?

I'm using rails 3 and I can't seem to check if a given instance is in a scope, see here:

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      |
+----+----------+-------------------------+-------------------------+------------------开发者_如何学JAVA-------+-----------+

I have a menu scope which looks like:

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

When I run it I get:

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      |
+----+------------------+------------------+------------------+-------------------+-----------+

Which is correct, but if I try to check if p is in the the menu scope using: Post.menu.exists?(p) I get true when it should be false

What is the proper way to find out if a given instance of something is in a scope?


Actually, I was able to solve it using that Array method of include? instead of exists?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜