# Trouble with has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/trouble-with-has-and-belongs-to-many/126476>\
**Category:** Rails\
**Created:** [January 18, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/trouble-with-has-and-belongs-to-many/126476 "2008-01-18T16:23:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [January 18, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/trouble-with-has-and-belongs-to-many/126476/1 "2008-01-18T16:23:47Z")

</div>

Hello,

I have this method in my helper that is supposed to return true if the  
campaignadmin is in the campaignadmins habtm.

def has\_access? campaign, campadmin  
begin  
#prints out the campadmin’s id  
puts campadmin.id  
campaign.campaignadmins.each do |ca|  
#prints out all the campadmins id’s in  
the campaign  
puts ca.id  
end  
campaign.campaignadmins.find(campadmin.id)  
puts “CAMPAIGN ADMIN EXISTS”  
return true  
rescue ActiveRecord::RecordNotFound  
return false  
end  
end

The thing is I get returned

3  
3

meaning that the ids are the same. However false is returned from the  
method meaning that the find method highlighted that it couldn’t find  
it. Now I assume that the find method is committing an sql call. But  
the problem lies in the fact that the campaignadmins have not been  
saved because other validation fails.

Does anyone know of a workaround apart from me having to do

campaign.campaignadmins.each do |ca|  
if ca.id = campadmin.id  
return true  
end  
end
