Sanity check needed for "include?" - help please

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

On 9/20/07, Max W. [email protected] 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.

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
, “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
n.co.uk – Brandable”, “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
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

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. [email protected]

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. [email protected]

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

(thanks for helping me out by the way)

On 9/20/07, Max W. [email protected] 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.

Bob S. wrote:

On 9/20/07, Max W. [email protected] 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?