Note that there are some explanatory texts on larger screens.

plurals
  1. POquery in rails3
    primarykey
    data
    text
    <p>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...?</p> <p>What i have:</p> <p>Models</p> <pre><code>users questions (with answer_id) answers votes (with answer_id and user_id) </code></pre> <p>model for users:</p> <pre><code>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 </code></pre> <p>model for questions:</p> <pre><code>belongs_to :user has_many :answers, :dependent =&gt; :destroy accepts_nested_attributes_for :answers, :reject_if =&gt; lambda { |a| a[:text].blank? }, :allow_destroy =&gt; true </code></pre> <p>model for answers:</p> <pre><code>belongs_to :question has_many :users, :through =&gt; :votes, :dependent =&gt; :destroy has_many :votes </code></pre> <p>model for votes:</p> <pre><code>belongs_to :answer belongs_to :user </code></pre> <p>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:</p> <pre><code>&lt;% for answer in @question.answers %&gt; &lt;% if current_user.voted_answer? (@question) %&gt; &lt;td&gt; &lt;strong&gt;&lt;%= answer.text %&gt;&lt;/strong&gt; &lt;/td&gt; &lt;% else %&gt; &lt;td&gt; &lt;%= answer.text %&gt; &lt;/td&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Thijs</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload