# Joining notes to users?

**URL:** <https://www.ruby-forum.com/t/joining-notes-to-users/109711>\
**Category:** Rails\
**Created:** [July 18, 2007, 7:21am UTC](https://www.ruby-forum.com/t/joining-notes-to-users/109711 "2007-07-18T07:21:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![indiehead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a587f6/32.png) [@indiehead](https://www.ruby-forum.com/u/indiehead)\
**Post date:** [July 18, 2007, 7:21am UTC](https://www.ruby-forum.com/t/joining-notes-to-users/109711/1 "2007-07-18T07:21:42Z")

</div>

hi, i’m working on an idea of mine using ruby on rails but totally  
stumped  
with the has\_many :notes, belongs\_to :users in ruby

could someone explain this to me?

basically every user will be able to create many notes (notes being a  
record  
with a body text field), but those individual notes will only relate to  
the  
user who created them.

so joe won’t be able to see sally’s note records

how do i do this?

can anyone point me to an easy example, done the authentication and  
everything else, just can’t get my head around this one.

appreciate it,

## John.

View this message in context:  
[http://www.nabble.com/joining-notes-to-users--tf4101141.html#a11662669](http://www.nabble.com/joining-notes-to-users--tf4101141.html#a11662669)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![indiehead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a587f6/32.png) [@indiehead](https://www.ruby-forum.com/u/indiehead)\
**Post date:** [July 18, 2007, 7:36am UTC](https://www.ruby-forum.com/t/joining-notes-to-users/109711/2 "2007-07-18T07:36:42Z")

</div>

Hi!  
By what i can understand is “Creator alone see his/her own notes”.  
For  
this you need to store creator\_id or user\_id in the notes table ans  
wherever  
you are displaying the notes just check if the note if from the user who  
has  
logged in.  
So, say session[:id] is having the id of the user who has logged in  
and  
in the rhtml somewhere you are having, @notes as collection of notes  
then  
while displaying you can write  
\<%for note in @notes  
\<%if note.user\_id == session[:id]%\>  
Show notes data here  
\<%end%\>  
\<%end%\>  
Hope this helps, 🙂

Thanks and regards,  
Swanand

---

<div class="post-metadata">

**Author:** ![indiehead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a587f6/32.png) [@indiehead](https://www.ruby-forum.com/u/indiehead)\
**Post date:** [July 18, 2007, 9:22am UTC](https://www.ruby-forum.com/t/joining-notes-to-users/109711/3 "2007-07-18T09:22:51Z")

</div>

if you just want to display the notes specific to the currently logged  
in user, you’d just use something like this (assuming current\_user  
returns the currently logged in user):

in your model:

user.rb  
class User \< ActiveRecord::Base

# each user will have many notes, each note will be associated with

# a user through a user\_id column in the notes table

has\_many :notes  
end

note.rb  
class Note \< ActiveRecord::Base

# a note belongs to a single

belongs\_to :user  
end

in your controller:

def notes  
#retrieve all the notes for the currently logged in user  
@notes = current\_user.notes  
end

and in your view:

\<% for note in @notes %\>  
\<%= note.title %\>  
\<%= note.body %\>  
…  
\<% end %\>

Mike

---

<div class="post-metadata">

**Author:** ![indiehead](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/a587f6/32.png) [@indiehead](https://www.ruby-forum.com/u/indiehead)\
**Post date:** [July 31, 2007, 12:19pm UTC](https://www.ruby-forum.com/t/joining-notes-to-users/109711/4 "2007-07-31T12:19:47Z")

</div>

Thanks Mike and Swanand, helped me out of a jam at the 11th hour.

Appreciate it,

John.
