I have a fairly standard blog app with the usual post and comments controller/models along with a user model for leaving comments.
So in the user model I have
has_many :comments
And in the the comments model
belongs_to :post
belongs_to :user
All very straight forward.
In the app I'm creating I want the comments to act a little differently. When the user leaves a comment for the first time then I will create a new comment. However I only want to allow one comment per post per user. If the user tries to leave another comment then we should only update the existing comment.
To do this I have added a this to the user model
has_many :posts, :through => :comment
And in the Posts controller I have the following in the show action.
if post = current_user.posts.find_by_permalink(params[:id])
@comment = post.comments.find_by_user_id(current_user)
else
@comment = Comment.new
end
This works in that it checks if the user already made a comment on this post and if so then it will result in an update to the comment rather than a new one posted.
But, the above code doesn't sit right with me. Is there a more elegant solution?