Hooking up restrictions

We're nearing the end of this exercise! We can finally start denying people based on their IP addresses if they've already voted, since we now know that they are indeed being recorded properly! The first thing we'll want to do is implement some sort of check for whether or not a user has already voted for a particular poll. For this, we'll return to our votes context (lib/vocial/votes/votes.ex) and implement an already_voted? function:

def already_voted?(poll_id, ip_address) do  votes = (from vr in VoteRecord, where: vr.poll_id == ^poll_id and vr.ip_address == ^ip_address)  |> Repo.aggregate(:count, :id)  votes > 0end

Here we take in the poll_id and the ip_address and just grab a count out of the database with the Repo.aggregate/3 ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.