mike raimondi

When (not) to Nest Resources in Rails

In Rails, nested resources allow us to generate RESTful routes for heirarchical resources. If we were designing a blog, there might be a posts and a comments resource. We might have this in our routes.rb:

resources :posts do
  resources :comments
end

Rails will then generate paths like this: posts/1/comments/1. This is all great and useful stuff, but what about something like users and dashboards?

More

The Inflection Point

Something strange happened over the past 10 weeks at Launch Academy. Invisibly, imperceptibly, I’ve turned from a software hobbyist into a software developer. This fact only came to light a few days ago, and I’d like to share the story of how that happened.

More

Mildly Complex Rails Seeding with YAML

It’s crunch time here at Launch Academy. Our projects have to be up and running with databases that will, you know, actually show off the functionality that we’ve worked 10 weeks to achieve. This means seeding, and depending on the complexity of our models and associations, seeding can go from a simple one-line script to, well, something like this:

module Seeders
  module Lessons
    class << self
      def seed
        Lesson.destroy_all
        lessons = []

        base_path = 'db/seed_data'
        Dir.glob(Rails.root.join(base_path, 'lessons', '*.yml')) do |file|
          lesson = YAML.load_file(file)
          this_lesson = Lesson.create(title: lesson['title'], summary: lesson['summary'])
          lessons << this_lesson
          lesson['activities'].each_with_index do |activity, index|
            completable = []

            if activity['type'] == 'Assignment'
              completable = Assignment.create do |assignment|
                assignment.title = activity['title']
                assignment.summary = activity['summary']
                assignment.instructions = activity['instructions']
                assignment.url = activity['url']
                assignment.assignment_type = activity['assignment_type']
              end
            elsif activity['type'] == 'Challenge'
              completable = Challenge.create do |challenge|
                challenge.title = activity['title']
              end
              activity['cards'].each do |card|
                new_card = Card.create do |c|
                  c.title = card['title']
                  c.instructions = card['instructions']
                  c.problem = card['problem']
                  c.solution_type = card['solution_type']
                  c.snippet = card['snippet'] if card['snippet'].present?
                end
                card['solutions'].each do |solution|

                  if card['solution_type'] == 'string'
                    sol = new_card.solution_strings.create do |solution_string|
                      solution_string.regex = solution['regex']
                      solution_string.canonical = true if solution['canonical']
                    end
                  elsif card['solution_type'] == 'position'
                    sol = new_card.solution_positions.create do |solution_position|
                      solution_position.start_position = solution['start_position']
                      solution_position.end_position = solution['end_position']
                    end
                  end

                  completable.cards << new_card
                end
              end
            end

            Activity.create do |lesson_activity|
              lesson_activity.completable = completable
              lesson_activity.lesson = this_lesson
              lesson_activity.position = index + 1
            end
          end
        end

        lessons
      end
    end
  end
end

This is the seeder for my project: Memworks. A major part of the app is lessons, each of which has assignments and challenges. There’s a lot of content, and my first approach was to just shove it all in big Ruby objects and require the relevant source files. I decided to shift to a YAML approach when the Ruby hashes became so large as to become unwieldy.

More

Getting Started with Backbone.js on Rails

Going from near-zero experience with front-end MVC frameworks to a functional Backbone app has been something of a rough ride. I thought I’d share some lessons I’ve learned along the way. Keep in mind that I’m still learning, so much of this will be wrong. Corrections are welcome.

More

Tick-Tock Synthesis

Synthesis is the phase of learning where we apply what we have learned in new ways. It is the point when we begin to understand how what we have learned connects with what we already know, and how those connections can be used to apply our new learning differently. I consider synthesis to be the most important part of my education at Launch Academy, so I’m extremely deliberate in the way I approach synthesizing new knowledge.

More