Skip to content

Instantly share code, notes, and snippets.

@alexgriff
Last active March 31, 2016 01:17
Show Gist options
  • Save alexgriff/9129d0471718119bf01072f853efd6f3 to your computer and use it in GitHub Desktop.
Save alexgriff/9129d0471718119bf01072f853efd6f3 to your computer and use it in GitHub Desktop.
# - create a candidate
# --------------------
obama = Candidate.create(first_name: "Barack", last_name: "Obama", birthdate:"08-04-1961",
position_running_for: "President", national_candidate: true)
# (0.1ms) begin transaction
# SQL (0.3ms) INSERT INTO "candidates" ("first_name", "last_name", "birthdate", "position_running_for", "national_candidate") VALUES (?, ?, ?, ?, ?) [["first_name", "Barack"], ["last_name", "Obama"], ["birthdate", "1961-04-08"], ["position_running_for", "President"], ["national_candidate", "t"]]
# (1.4ms) commit transaction
# => #<Candidate id: 1544, position_running_for: "President", first_name: "Barack", last_name: "Obama",
# birthdate: "1961-04-08", national_candidate: true >
# - build a voter from that candidate
# -----------------------------------
# the has_one association gives us the build_voter method
# (the voter model validates a ssn for a voter so had to provide one)
#
# notice that the a voter object is returned with the voterable_id column set to
# Candidate Obama's id, and the voterable_type is "Candidate"
obama.build_voter(first_name: obama.first_name, last_name: obama.last_name,
birthdate: obama.birthdate, address:"1600 Pennsylvania Avenue NW",
state: "Washington, DC", ssn: "123-45-6789")
# Voter Load (0.2ms) SELECT "voters".* FROM "voters" WHERE "voters"."voterable_id" = ? AND "voters"."voterable_type" = ? LIMIT 1 [["voterable_id", 1545], ["voterable_type", "Candidate"]]
# => #<Voter id: nil, ssn: "123-45-6789", birthdate: "1961-04-08", email: nil,
# address: "1600 Pennsylvania Avenue NW", state: "Washington, DC", registration_status: nil,
# first_name: "Barack", last_name: "Obama", voterable_id: 1545, voterable_type: "Candidate"
# - our candidate, acting as a voter, can now do the things a voter can do
# ------------------------------------------------------------------------
# registration saves a voter to the db
# I use the as_a_voter method defined above, but could also say obama.voter
obama.as_a_voter.register
# (0.1ms) begin transaction
# SQL (1.8ms) INSERT INTO "voters" (...)
# (5.2ms) commit transaction
# => true
vote = obama.as_a_voter.cast_vote_for(obama)
# (0.1ms) begin transaction
# Vote Exists (0.3ms) SELECT 1 AS one FROM "votes" WHERE ("votes"."voter_id" = 647 AND "votes"."candidate_id" = 1545) LIMIT 1
# Vote Load (0.2ms) SELECT "votes".* FROM "votes" WHERE "votes"."voter_id" = ? [["voter_id", 647]]
# SQL (0.2ms) INSERT INTO "votes" ("candidate_id", "voter_id") VALUES (?, ?) [["candidate_id", 1545], ["voter_id", 647]]
# (1.5ms) commit transaction
# => #<Vote id: 1635, voter_id: 647, candidate_id: 1545>
# - let's check out that vote and that all the associations are intact
# --------------------------------------------------------------------
vote.candidate
# => #<Candidate id: 1545, position_running_for: "President", first_name: "Barack", last_name: "Obama", birthdate: "1961-04-08", ...>
vote.voter
# => #<Voter id: 647, ssn: "123-45-6789", birthdate: "1961-04-08",address: "1600 Pennsylvania Avenue NW",
# state: "Washington, DC",registration_status: true, first_name: "Barack", last_name: "Obama",
# voterable_id: 1545, voterable_type: "Candidate">
# we can see a voter's belongs to association by using #voterable
# note that voter.candidate or voter.citizen would error out
vote.voter.voterable
# => #<Candidate id: 1545, position_running_for: "President", first_name: "Barack", last_name: "Obama", birthdate: "1961-04-08", ...>
vote.voter.voterable == vote.candidate
# => true
# :)
# none of the previous functionality is gone,
# a candidate still has the #voters method which returns an AR Collections Proxy
# because of the has_many :voters
obama.voters.first == obama.as_a_voter
# => true
# :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment