Forum: Ruby on Rails joining notes to users?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2a61fac4987d6ffa7298b8c0dca0e245?d=identicon&s=25 indiehead (Guest)
on 2007-07-18 07:21
(Received via mailing list)
hi, i'm working on an idea of mine using ruby on rails but totally
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
with a body text field), but those individual notes will only relate to
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,

View this message in context:
Sent from the RubyOnRails Users mailing list archive at
833bc92fe8f46a9bfebae5afb65f7507?d=identicon&s=25 Swanand Deodhar (swanand)
on 2007-07-18 07:36
(Received via mailing list)
   By what i can understand is "Creator alone see his/her own notes".
this you need to store creator_id or user_id in the notes table ans
you are displaying the notes just check if the note if from the user who
logged in.
   So, say session[:id] is having the id of the user who has logged in
in the rhtml somewhere you are having, @notes as collection of notes
while displaying you can write
   <%for note in @notes
       <%if note.user_id == session[:id]%>
             Show notes data here
  Hope this helps, :)

Thanks and regards,
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2007-07-18 09:22
(Received via mailing list)
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:

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

class Note < ActiveRecord::Base
   # a note belongs to a single
   belongs_to :user

in your controller:

def notes
#retrieve all the notes for the currently logged in user
@notes = current_user.notes

and in your view:

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

760e5c90000057a6860986b69b77c400?d=identicon&s=25 John Griffiths (indiehead)
on 2007-07-31 12:19
Thanks Mike and Swanand, helped me out of a jam at the 11th hour.

Appreciate it,

This topic is locked and can not be replied to.