Skip to content

Instantly share code, notes, and snippets.

@jason
Created March 3, 2013 00:36
Show Gist options
  • Save jason/5073930 to your computer and use it in GitHub Desktop.
Save jason/5073930 to your computer and use it in GitHub Desktop.
VoteTypes.name holds whether the vote is an aye, nay, or abstain. @ resolution has Resolution.find(params[:id}). Votes has the vote_type_id, the member_id (who it is that's voting) and the resolution_id
td = @vote_types[@resolution.votes.where(:member_id => n).first.vote_type_id - 1].name
@indirect
Copy link

indirect commented Mar 3, 2013

Honestly it seems like VoteType should be a constant inside Vote instead of its own class with a mapping to name, but without doing that, here's roughly what it should look like:

class Vote
  belongs_to :vote_type
  def vote_name
    self.vote_type.name
  end
end

I've never used slim, but here's the ERB equivalent:

<% @resolution.votes.each do |vote| %>
  <td><%= vote.member.name %> voted <%= vote.vote_name %></td>
<% end %>

@indirect
Copy link

indirect commented Mar 3, 2013

Oh and don't forget to load the resolution, votes, members, and vote_types up front when you query in the controller. If you don't, your view might trigger an extra SQL query every time you try to access vote.member or vote.vote_name.

@resolution = Resolution.include(:votes => [:member, :vote_type]).find(params[:id])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment