0

I am trying to search a dataset for a value. If the dataset contains the value, the app does something. If it does not, the app does something else.

The data breaks down like this: I have affiliates and users, each with a HABTM relationship to the other. I have a page where the user can sign up for affiliates, which are displayed as a group of checkboxes.

I want the checkboxes for all the affiliates a user has currently signed up for checked.

Here's the code for the view (in HAML)

- @affiliates.each do |a|
  %li
    %label{ :for => "affiliate_#{a.id}"}= a.name
    - if @current_user.affiliates.select{ |ua| ua.id == a.id }
      = check_box_tag "affiliate_list[#{a.id}]", 1, true, {:id => "affiliate_#{a.id}"}
    - else
      = check_box_tag "affiliate_list[#{a.id}]", 1, false, {:id => "affiliate_#{a.id}"}

This code always returning true, and thus, checks boxes, even if a user has not signed up for an affiliate.

I looked up the .select method, but I keep coming up with the form helper stuff.

niton
  • 8,771
  • 21
  • 32
  • 52
Kevin Whitaker
  • 457
  • 1
  • 6
  • 12

1 Answers1

0
if @current_user.affiliates.include?(a)
elmac
  • 228
  • 1
  • 5
  • @current_user.affiliates.select{ |ua| ua.id == a.id } returns [] at best which is not false... so @current_user.affiliates.select{ |ua| ua.id == a.id } == [] would be correct, but just looks plain ugly and isn't rails all about beatiful code ;) – elmac Aug 02 '10 at 17:04