finding users in a group returned query

I'm relatively new to rails and am setting up a system which contains
bands and users. Both bands and users has and belongs to many of the
other.

In the bands "show" view, I want to list all the users within a band.
I do this by using the code:

<% for user in @band.users %>

and iterating through the results.

However, I want to display a special div if the current user's id
(stored in the session variabel "@current_user.id") is within the
returned results. I have unsuccessfully tried doing the following:

<% for user in @band.users %>

  Member name:<%=h user.login %> <br>

  <% if user.user_id == @current_user.id %>
  *DISPLAY SPECIAL DIV*
  <% end %>

<% end %>

I can't understand why this isn't working. Can anyone advise me on
this?

I think you mean this instead?

if user.id == @current_user.id

(instead of user.user_id == .....)

Jeff
softiesonrails.com
purpleworkshops.om

the line

<% if user.user_id == @current_user.id %>

should be

<% if user.id == @current_user.id %>

Maybe you're storing the user_id within session[:user_id]?

session[:user_id] == user.id

I think my relationships may be set up incorrectly but I'm not sure
why. I guess I'd be best to give a bit of background.

I'm setting up a musicians network for my local area. There are bands
and users as described above, and these are linked with the table
"bands_users" which has three fields: id, band_id, and user_id. I am
also working off the Beast forum so I thought it would be as easy to
use their user table and configuration since its already set up.

So, what I find strange is that, when I want to return the user id
like above, I must use "user.user_id". I believe I should be able to
simply state user.id but doing so returns the value from "id" (note:
NOT user_id - just the unique id from the bands_users table).

I also have a similar relationship set up between bands and gigs, but
this works as desired.

Does anyone have any ideas why this would be messing up? Would the
fact that I'm taking the data from the Beast Forum's users be causing
problems. Any help, as ever, is appreciated.

I think my relationships may be set up incorrectly but I'm not sure
why. I guess I'd be best to give a bit of background.

If you thing that's the case, then maybe showing us how you're setting
them up would enlighten everyone.

Fred

If its enlightenment you seek, then I'd suggest reading the entire
post. Regardless, here is the code if you couldn't figure it out:

band.rb

If its enlightenment you seek, then I'd suggest reading the entire
post. Regardless, here is the code if you couldn't figure it out:

Shot in the dark: does your join table between bands and users have an
id column ? It shouldn't. If rails is just doing a select * then the
id from the join table could easily clobber the id from the users table.

Fred

You have only one has and belongs to many relationship as shown below:

has_and_belongs_to_many :bands

Both Obie Fernandez in his Rails Way and Ryan Bates in his
ActiveRecords screen-cast Episode 1 suggest to use "has many through"
associations instead since it is much more flexible. I do not have a
lot of experience with Rails but have generally avoided using has and
belongs to many due to its restrictive nature. You already are using
has many through without any problems right?

Hope this helps.

Bharat

Its begun to work now (added an else statement) but thanks for the
advice anyway.

What I'd like to is to perform a check to see if a user is in a band
within a controller. I presume I should put something along the
following in the bands controller:

def is_member_in_band
    return if admin?
    for user in @band.users
      if user.id == @current_user.id
        return true
      end
    end
end

Is there way of simply checking if the collection of users in
@band.users contains the current user's id without having to loop
through them?

Thanks to everyone who's replied so far.

This returns the user object if it exists:

@band.users.find_by_id(@current_user.id)

It will return nil if the user is not part of the band not found.

Does this help?

Jeff
purpleworkshops.com

That works perfectly - Thanks a lot!!