开发者

Filtering records in controller

开发者 https://www.devze.com 2022-12-27 22:44 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消