# Sanity check needed for "include?" - help please

**URL:** <https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099>\
**Category:** Rails\
**Created:** [September 20, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099 "2007-09-20T16:06:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/1 "2007-09-20T16:06:27Z")

</div>

I’ve got two objects, @original\_article and @article, both are instances  
of Article. Both have a list of ‘links’, where links are other  
articles. I want to test if @original\_article has @article in its links  
already.

This is what i’m doing - the if test is returning false even if @article  
has already been added to @original\_article’s links. Can anyone see  
what i’m doing wrong? This is driving me crazy…

```
if @original_article.links.include? @article
  flash[:message2] = "Link already added to this article!"
else
  @original_article.links << @article
```

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/2 "2007-09-20T16:30:40Z")

</div>

On 9/20/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if @original_article.links.include? @article
> flash[:message2] = "Link already added to this article!"
> else
> @original_article.links << @article
> 
> ```

Can’t tell what the problem is from the information given. include?  
can be used in this fashion, so the problem is due to something else.

Do something like this in script/console and show us the output:

@original\_article = Article.find(123)  
@original\_article.links  
@article = Article.find(456)  
@original\_article.include? @article

Use appropriate id numbers. That way we can see what’s going on.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/3 "2007-09-20T18:05:21Z")

</div>

Bob S. wrote:

> Do something like this in script/console and show us the output:
> 
> @original\_article = Article.find(123)  
> @original\_article.links  
> @article = Article.find(456)  
> @original\_article.include? @article
> 
> Use appropriate id numbers. That way we can see what’s going on.

OK, thanks - here’s my console output. I don’t understand why include?  
is returning false…

> > @original\_article = Article.find\_by\_id(59)  
> > =\> #\<Article:0x447c350 @attributes={“title”=\>“Web of ancestry | Special  
> > reports | Guardian Unlimite  
> > d”, “points”=\>“17”, “tags”=\>“”,  
> > “url”=\>“[Web of ancestry | UK news | The Guardian](http://www.guardian.co.uk/military/story/0,,2144101,00.html)”  
> > , “id”=\>“59”, “added\_at”=\>“2007-09-11 11:49:57”, “user\_id”=\>“26”}\>

> > @original\_article.links  
> > =\> [#\<Article:0x44796a0 @attributes={“link\_id”=\>“125”, “title”=\>“Port  
> > City, Arnolfini, Bristol | A  
> > rt & Architecture | Guardian Unlimited Arts”, “points”=\>“2”, “tags”=\>“”,  
> > “url”=\>“[http://arts.guardia](http://arts.guardia)  
> > [n.co.uk – Brandable](http://n.co.uk/art/visualart/story/0,,2171969,00.html)”, “id”=\>“125”,  
> > “added\_at”=\>“2007-09-20 14:53:09”, “ar  
> > ticle\_id”=\>“59”, “user\_id”=\>“1”}\>]

> > @article = Article.find\_by\_id(125)  
> > =\> #\<Article:0x4476928 @attributes={“title”=\>“Port City, Arnolfini,  
> > Bristol | Art & Architecture |  
> > Guardian Unlimited Arts”, “points”=\>“2”, “tags”=\>“”,  
> > “url”=\>“[http://arts.guardian.co.uk/art/visuala](http://arts.guardian.co.uk/art/visuala)  
> > rt/story/0,2171969,00.html”, “id”=\>“125”, “added\_at”=\>“2007-09-20  
> > 14:53:09”, “user\_id”=\>“1”}\>

> > @original\_article.links.include? @article  
> > =\> false

In case this is relevant, articles and links have a  
has\_and\_belong\_to\_many relationship, perhaps @original\_articles.links is  
being interpeted as “links that own this article?” rather than “links  
that belong to this article”? Maybe i’ve defined the relationship  
incorrectly? Here’s the definition (to make things more confusing, a  
link is itself an article):

has\_and\_belongs\_to\_many :links,  
:class\_name =\> “Article”,  
:foreign\_key =\> “article\_id”,  
:association\_foreign\_key =\> “link\_id”,  
:join\_table =\> “articles\_links”

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/4 "2007-09-20T18:25:55Z")

</div>

can you go to script/console and try this

@original\_article = Article.find(59)  
@original\_article.link\_ids  
@article = Article.find(125)  
@original\_article.link\_ids.include?(@article.id)

On Sep 20, 11:05 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/5 "2007-09-20T18:41:12Z")

</div>

Andrew B. wrote:

> can you go to script/console and try this
> 
> @original\_article = Article.find(59)  
> @original\_article.link\_ids  
> @article = Article.find(125)  
> @original\_article.link\_ids.include?(@article.id)
> 
> On Sep 20, 11:05 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

That returns true (ie it’s correct)- thanks! But why didn’t my original  
work?

(thanks for helping me out by the way)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/6 "2007-09-20T19:37:12Z")

</div>

On 9/20/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That returns true (ie it’s correct)- thanks! But why didn’t my original  
> work?
> 
> (thanks for helping me out by the way)

I’m stumped. I tried this on one of my own HABTM associations and  
include? was working fine when comparing objects (not just ids).

For kicks what does this show:

@original\_article.links.first == @article

In my testing, this returns true.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 20, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/sanity-check-needed-for-include-help-please/115099/7 "2007-09-20T20:56:17Z")

</div>

Bob S. wrote:

> On 9/20/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > That returns true (ie it’s correct)- thanks! But why didn’t my original  
> > work?
> > 
> > (thanks for helping me out by the way)
> 
> I’m stumped. I tried this on one of my own HABTM associations and  
> include? was working fine when comparing objects (not just ids).
> 
> For kicks what does this show:
> 
> @original\_article.links.first == @article
> 
> In my testing, this returns true.

It returns ‘nil’ for me. I’ve never seen a boolean equals test return  
nil before, is that a ruby thing?
