开发者

query in rails3

开发者 https://www.devze.com 2023-02-21 23:05 出处:网络
In another question that i asked recently i got a really good answer and the code worked... But i do not know exactly why it works... Now i have a similar problem, but don\'t know how to solve it...?

In another question that i asked recently i got a really good answer and the code worked... But i do not know exactly why it works... Now i have a similar problem, but don't know how to solve it...?

What i have:

Models

users
questions (with answer_id)
answers
votes (with answer_id and user_id)

model for users:

has_many :questions
has_many :votes
def can_vote_on? (question)
    !question.answers.joins(:votes).where('votes.user_id = ?', id).exists?
  end

def voted_answer? (question)
   (what to do here...?) 
  end

model for questions:

belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? }, :allow_destroy => true

model for answers:

belongs_to :question
has_many :users, :through => :votes, :dependent => :destroy
has_many :votes

model for votes:

belongs_to :answer
belongs_to :user

In my question view i want to make a text bold when the current_used has voted on that specific answer. So how do i finish this:

<% for answer in @question开发者_JS百科.answers %>
 <% if current_user.voted_answer? (@question) %>
  <td>
   <strong><%= answer.text %></strong> 
  </td> 
 <% else %>
  <td>
   <%= answer.text %>
  </td> 
 <% end %>
<% end %>

Thijs


you may do this

<% for answer in @question.answers %>
  <% if answer.votes.index{|vote| vote.user_id == current_user.id} %>
    <td>
    <strong><%= answer.text %></strong> 
    </td> 
  <% else %>
    <td>
    <%= answer.text %>
    </td> 
  <% end %>
<% end %>

UPDATE

more logical variant create voted_by_user? function in class Answer

class Answer
  def voted_by_user?(user)
    voits.where('votes.user_id = ?', user.id).exists?
  end
end

<% @question.answers.each do |answer| %>
  <td>
    <% if answer.voted_by_user?(current_user) %>
      <strong><%= answer.text %></strong> 
    <% else %>
      <%= answer.text %>
    <% end %>
  </td> 
<% end %>


It sounds like you just want the opposite result of can_vote_on?, i.e. if a user cannot vote on an answer (can_vote_on? returns false), then it means that they already voted (voted_answer? should return true in this case) and vice versa.

One way to solve this is to have voted_answer? return the negation of can_vote_on:

def voted_answer? (question)
    !can_vote_on? question
end

Or of course you could use the query you used in can_vote_on? without the negation:

def voted_answer? (question)
    question.answers.joins(:votes).where('votes.user_id = ?', id).exists?
end

But I would prefer the first solution due to the DRY principle.

UPDATE

I was wrong about the negation. In this case you're dealing with a specific answer, not all of them.

In your model you'll want the following:

def voted_answer? (answer)
    answer.votes.where('votes.user_id = ?', id).exists?
end
0

精彩评论

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