Filtering records in controller
So in my model, there is a field user_id - which holds the ID of the user who created the record. To display the ID of the current user, I have @current_user.id
My question is this, in the controller I want @p开发者_JAVA百科osts to only have records created by @current_user.id
how can I do this?
As what ryeguy had mentioned, you can add has_many :posts
into User model, or alternatively, do
@posts = Post.find_all_by_user_id(@current_user.id)
but this is so much more troublesome...
hope it helps =)
Assuming that user has_many posts
, you can do this:
@current_user = get_current_user()
@posts = @current_user.posts
精彩评论