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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 << @articleCan''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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter 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"=>"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 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 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Bob Showalter 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"=>"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 > 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 > 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Bloom 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 Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>That returns true (ie it''s correct)- thanks! But why didn''t my original work? (thanks for helping me out by the way) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Andrew Bloom 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 Williams <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On 9/20/07, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---