Rails, if instance is in a scope?
Posted
by Joseph Silvashy
on Stack Overflow
See other posts from Stack Overflow
or by Joseph Silvashy
Published on 2010-03-29T04:49:33Z
Indexed on
2010/03/29
4:53 UTC
Read the original article
Hit count: 404
ruby-on-rails
|ruby-on-rails3
I 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 |
+----+----------+-------------------------+-------------------------+-------------------------+-----------+
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?
© Stack Overflow or respective owner