Forum: Ruby on Rails show only notes for current user

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a61fac4987d6ffa7298b8c0dca0e245?d=identicon&s=25 jag (Guest)
on 2007-07-30 22:49
(Received via mailing list)
hi, i've written an app but failing on one pivotal part,

i have a user model built, and a notes model built

now, once the user navigates to the notes view they can create notes
and they will only be available to that user.

i've got it saving the user_id to the note record, but not filtering
out only notes for that current user.

any ideas?

sample code

<% for note in @notes %>
  <tr>
  <% for column in Note.content_columns %>
    <td><%=h note.send(column.name) %></td>
  <% end %>
    <td><%= link_to 'Show', :action => 'show', :id => note %></td>
    <td><%= link_to 'Edit', :action => 'edit', :id => note %></td>
    <td><%= link_to 'Destroy', { :action => 'destroy', :id =>
note }, :confirm => 'Are you sure?', :method => :post %></td>
  </tr>
<% end %>
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-31 00:12
(Received via mailing list)
filter it in the controller:

def show
  @notes = Note.find(:all, :conditions => ["user_id = ?",
@current_user.id]
end
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-31 03:40
(Received via mailing list)
On 7/31/07, jemminger <jemminger@gmail.com> wrote:
>
>
> filter it in the controller:
>
> def show
>   @notes = Note.find(:all, :conditions => ["user_id = ?",
> @current_user.id]
> end



Or to use the has_many :notes association in the User model

def show
  @notes = @current_user.notes
end
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2007-07-31 04:48
(Received via mailing list)
ah yes, even better!
This topic is locked and can not be replied to.