-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create likes #3
base: master
Are you sure you want to change the base?
Create likes #3
Conversation
@like = @post.likes.where(user_id: current_user.id).first_or_create | ||
|
||
unless @like.save | ||
puts @like.errors.inspect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove comment?
app/models/user.rb
Outdated
has_many :posts, through: :likes | ||
|
||
def likes?(post) | ||
post.likes.where(user_id: id).any? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be: likes.find_by(post_id: post.id).present?
end | ||
|
||
def destroy | ||
@like = @post.likes.where(user_id: current_user.id).destroy_all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be:
@like = current_user.likes.find_by(post_id: @post.id)
@like.destroy
head 204
The first change because you always want to think about operating on the current users likes because if you don't, you could be operating on any like. You cover that case here but I feel like this format makes the behavior/intention more clear.
The head 204
is nice because you're saying there's no content to send down
before_action :set_post | ||
|
||
def create | ||
@like = @post.likes.where(user_id: current_user.id).first_or_create |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here, putting the focus on the current user.
@@ -9,5 +9,10 @@ class User < ApplicationRecord | |||
validates :password, presence: true | |||
validates :authentication_token, uniqueness: true | |||
|
|||
has_many :posts | |||
has_many :likes | |||
has_many :posts, through: :likes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is what I want to be doing here?
dd3a047
to
121314d
Compare
25f6254
to
1603329
Compare
This sets up likes. I would like to be able to use the likes? method from the user model on the front end to correctly show whether the user has liked the specific post. I'm just not sure how to go about it.