# Problems with getting firstname from user in habtm relations

**URL:** <https://www.ruby-forum.com/t/problems-with-getting-firstname-from-user-in-habtm-relations/111639>\
**Category:** Rails\
**Created:** [August 9, 2007, 11:49am UTC](https://www.ruby-forum.com/t/problems-with-getting-firstname-from-user-in-habtm-relations/111639 "2007-08-09T11:49:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesper\_Christiansen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58f4c7/32.png) [@Jesper\_Christiansen](https://www.ruby-forum.com/u/Jesper_Christiansen)\
**Post date:** [August 9, 2007, 11:49am UTC](https://www.ruby-forum.com/t/problems-with-getting-firstname-from-user-in-habtm-relations/111639/1 "2007-08-09T11:49:33Z")

</div>

Hey there…

In my view i have:

\<%= @event.invited %\>

In my event.rb I have:

class Event \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users

def invited  
users.compact.join(", ")  
end  
end

In my users.rb I have:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :events  
end

The output I get in my view, when calling @event.invited is: #, #

My question is, how do I get it to output the names of the users,  
instead of just the # … I have tried doing a:

for user in users do  
user.first\_name  
end

But that outputs # #

What am I doing wrong?

Thank you soo much in advance!  
-Jesper

---

<div class="post-metadata">

**Author:** ![Jesper\_Christiansen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/58f4c7/32.png) [@Jesper\_Christiansen](https://www.ruby-forum.com/u/Jesper_Christiansen)\
**Post date:** [August 9, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/problems-with-getting-firstname-from-user-in-habtm-relations/111639/2 "2007-08-09T16:49:14Z")

</div>

Jesper,

Looking at the info you provided, there’s nothing which actually  
retrieves something from the database.

Lets say you’ve done:

@event = Event.find(:all)

then with your model as is you can refer to then do:

invited = @event.users (assuming that merely the presence of a  
relationship between an event and a user means they’re invited).

if you’re only after the name then you could do:

invited = @event.users.collect! {|u| u.name}.join(’,’)

You might also changing your model to a :has\_many :though approach  
which lets you easily store attributed in the join table which, for  
event management, would be very appropriate. Then you can have a  
status code in the join table like “invited”, “accepted”, “scumbag  
didn’t show”, “spam with many offers” or whatever you want.

If you follow this approach then your model would be something like:

class Event  
:has\_many :event\_records  
:has\_many :users, :though =\> :event\_records

In your controller  
@invited = @event.users.find(:conditions =\> “event\_records.status =  
‘invited’”)

And in your view  
\<% for invited in @invited %\>  
\<%= invited.name %\>  
\<% end %\>

Hope this helps (at least a little)

Cheers, --Kip
