Joining notes to users?

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
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

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, :slight_smile:

Thanks and regards,
Swanand

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

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

Appreciate it,

John.