#RailsNinjaSchool
Explore tagged Tumblr posts
Text
week, day = 3, 3
Day 13 of App Academy = Day 1 of Rails
We've finally dived into the Rails pool (which actually sounds rather painful, but it isn't, I swear). Monday was SQL day, Tuesday was advanced SQL day which basically involved writing a simple ORM (object-relational mapping) and today, we got to play with a real ORM: Active Record. So given our recent trajectory and the fact that web app design nearly always involves database management, I suppose it's only logical that the first part of rails we go into with any degree of depth is database management. And in Rails, that is done in large part with ActiveRecord.
Essentially, ActiveRecord is a Ruby library that allows us to get our model and database working together. It follows the general active record pattern and adds inheritance and associations for an even more versatile and powerful system. We learned about using migrations to create and alter tables and table columns. We wrote associations to make our data more easily accessible and validations for user-supplied input to make sure our database didn't end up a giant mess.
Just like yesterday, the last thing my partner and I wrote today was definitely the coolest thing of the day. All day we had been writing associations and validations. The validations mostly looked like:
class ShortenedUrl < ActiveRecord::Base validates :submitter_id, presence: true validates :short_url, presence: true, uniqueness: true validates :long_url, presence: true, length: { maximum: 1024 } end
Pretty straightforward stuff: make sure the submission is linked to a valid user, make sure the submission includes the necessary components for it to be useful, make sure we're not duplicating things that shouldn't be duplicated, and make sure that we're not letting users enter novels in place of URLs.
But then we were tasked with preventing a user from using our service faster than a human should be able to; basically, catching bots. There is no built-in validation for this (that I am aware of, although, maybe there should be?), so we got to write it ourself.
Ultimately, we wrote a helper method that checks to see how many submissions have been made by the current user in the last minute and then using that to determine whether the user's behavior is symptomatic of a non-human. We then called that method in its own validation statement to prevent a non-human (or perhaps super-human) user from continuing to create new entries.
class ShortenedUrl < ActiveRecord::Base ... validate :not_a_spammer ... private def not_a_spammer if submitter.num_recent_submissions > 5 errors[:spammer] << "too many submissions in the last minute (>5)" end end end
(the helper method lives in the User class)
class User < ActiveRecord::Base def num_recent_submissions submitted_urls.select(:id).where("created_at > '#{1.minute.ago}'").count end end
As expected, as soon as we tried to submit our 6th shortened URL in under a minute, we got an error and were shut down.
While this was super cool, there was a lot of work that went into building the associations that made this little snippet of code work so well. And that was less fun, but ultimately very rewarding. I definitely approve of the over-arching flow of this week. Each day makes the previous day's tedium make perfect sense. And without the previous day's tedium, the current day would be full of mystery, wonder, and automagical moments, which is not what you want when you're trying to become a Rails ninja.
Also, lecture was super long because difficult concepts. But Jeff was really on his game. Look for Jeff quotes in the coming days.
0 notes