私は次のものを持っています (Vote
との多態的な関連付けとしてPost
) :
votes_controller.rb:
if already_voted?
@vote = @votable.votes.find_by_user_id(current_user.id)
@vote.destroy
end
posts/_vote_form.html.erb:
<div class="vote-form">
<% if @post.votes.exists?(:user_id => current_user.id) %>
<% if @post.votes.find_by_user_id(current_user.id).polarity == 1 %>
<%= form_for ([@post, @post.votes.find_by_user_id(current_user.id)]),
method: :delete,
remote: true do |f| %>
(etc).
votes.find_by_user_id(current_user.id)
投稿ビューと投票コントローラーの両方を次のように置き換えたいと思います。
def vote_by_current_user
self.votes.find_by_user_id(current_user.id)
end
したがって、次のように読みやすくなります。
@vote = @votable.vote_by_current_user
また<% if @post.vote_by_current_user.polarity == 1 %>
そのメソッドを両方で利用できるようにする方法がよくわかりません。
助言がありますか?