Trouble with has_and_belongs_to_many

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs