Sowing Seeds with Sinatra

A tree project with Sinatra + Active Record + SQLite

Rachel Levinson
2 min readJun 24, 2021
Image Source: https://underware.nl/case-studies/subpixel-ascii-plus-art/

It’s been exciting to finally have the competency/comfort while coding to be able to listen to music while working. I focus better while audio is driving me forward!

For my Flatiron School Sinatra project, I built an app for Greenpoint’s Adopt-A-Tree program. The CSS is “enough” but I’m proud of the app structure. One thing I found helpful was attending Study Groups and digging into aspects of Active Record I didn’t explicitly learn in the course.

Active Record Validations

Source: Active Record Guides

After spending so much energy in Ruby building my own methods, it was nice to work on a project where Active Record can offer some of the heavy lifting. While the curriculum focused on AR’s uses for migrations and associations, I also found the validations accessible & load-bearing. Specifically, using Active Record validations to build my sign-in and login pages was vital.

HTML client-side validation is easily available by using input type="email" or input type="password". However, it’s stronger to also include server-side validation through something like Active Record Validations.

Some different, useful validation helper methods:

  • presence: true checks to see if your association (e.g. :email) is present before the object is instantiated
  • uniqueness: true checks to see if your association already exists before creating/saving/updating (great for unique usernames)
  • length: {minimum: XX} assigns a parameter of how long the association must be. Can be a minimum or maximum or range.
  • format: can be used with regex to validate the content matches certain rules. I used this regex string from mailtrap’s blog /\A[A-Za-z0–9+_.-]+@[A-Za-z0–9.-]+\z/ . This is a great way to provide server-side validations to backup client-side tools.

All of these validations are so helpful once set up in your Models. Whenever you one of the methods below in your Controller, Active Record automatically runs the validations before following through:

  • create
  • save
  • update

This is a great way to safely and cleanly build an app without cluttering up with too many custom methods!

Crown Shyness is when a tree *validates* whether another tree is near them before branching out… I think?? Image Credit: https://www.nhm.ac.uk/discover/crown-shyness-are-trees-social-distancing.html

Thanks for joining me! Here’s the project’s github :)

--

--

Rachel Levinson

Rachel is an occasional writer, learning creative coder, + constant focaccia baker. https://www.rachellevinson.com/