views:

9

answers:

1

First: Sorry for the ambiguous question, I'm not fully versed on Rails parlance yet.

I am trying to search a dataset for a value. If the dataset contains the value, I'd like the app to do something. If it doesn't, the app needs to do 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 signup for affiliates, which are displayed as a group of checkboxes. What I'm looking for is a way to have 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}"}

The problem with this code, however, is that it is always returning true, and thus, checking boxes, even if a user hasn't signed up for an affiliate.

I've tried looking up the .select method, but I keep coming up with the form helper stuff. Can someone point me in the right direction?

Thanks

A: 
if @current_user.affiliates.include?(a)
elmac
@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 uglyand isn't rails all about beatiful code ;)
elmac