So I want every user to see only his posts. Always.
If I do Post.find() I want to add :conditions => ['user_id = ?',
session[:user_id]] for every call to find. I call find in a lot of
actions, like find(params[:id]) or find_by_title('hello') and other
variants of find.
So my questions: can i add my condition to every call to find by adding
it only one time somewhere?
So I want every user to see only his posts. Always.
If I do Post.find() I want to add :conditions => ['user_id = ?',
session[:user_id]] for every call to find. I call find in a lot of
actions, like find(params[:id]) or find_by_title('hello') and other
variants of find.
So my questions: can i add my condition to every call to find by adding
it only one time somewhere?
You can scope your find on your user. I assume that you are extracting
your user, probably with something like:
@current_user = User.find session[:user_id]
If so, then replace all your Post finds which restrict to the user with
a find on @current_user.posts, such as:
@user_posts = @current_user.posts
All #find calls on the Post class can be applied to the
@current_user.posts association proxy, so to get the last 5 posts for
this user: