W8D3 | Final Project Day 1
We finally made it to the "final projects" phase of App Academy. My project a clone of Bill Pin. In summary, it's an app that allows you to keep track of bills you share with others. You can post a bill, share it with others, record payments, and BillPin will show an up-to-date total of how much you and your friends owe each other.
The first steps I took to making my app were:
Authentication I decided to roll my own auth instead of using Devise, just to make sure I remembered all the steps. Later on I plan to add omniauth so that users can sign up through email
Initial database and models for users, bills, and bill shares.
I spent awhile coming up with some of the custom sql queries for calculating the subtotals for how much a user owes and is owed by their friends. I learned that when you use ActiveRecord's #find_by_sql or #select for aggregated values (e.g. sum, count) with an alias, it returns an array of objects that have an attribute with the name of the alias.
What does this mean? It means that if your query selects SUM(val) AS subtotal, you can call results.first.subtotal to get the caluclated subtotal returned by the query.
def loan_subtotals # returns an array of user objects with attribute # "amt_loaned" that represents $ owed *to* user # [ <user1>, <user2> ] User.find_by_sql([<<-SQL, user_id: self.id]) SELECT users.*, SUM(bill_shares.amount) AS amt_loaned FROM bills LEFT JOIN bill_shares ON bills.id = bill_shares.bill_id LEFT JOIN users ON users.id = bill_shares.debtor_id WHERE bills.lender_id = :user_id GROUP BY users.id SQL end # user_loans = user.loan_subtotals # => [#<User id: 1 ... >] # user_loans.first.amt_loaned # => 50.0

















