CNK's Blog

Notes for Boulder RailsBridge Workshop

I am going to be teaching at a RailsBridge workshop in 2 weeks and thought I should work through the course at least once before I stand up in front of an audience! I’ll need to work through the install instructions for some other operating systems (at least the Windows instructions since that is where I have the least experience) but thought I would start by making sure my new Mac conformed to their standard setup. I had already set up Xcode, RVM, and Rails as described in a previous blog post.

So I reviewed the install instructions. Things have gotten pretty hairy on the Mac side but mostly looks OK - except they didn’t have anything about the Xcode command line tools killing terminfo files.

Since RailsBridge recommends KomodoEdit, I thought I should try it. One thing to note, it offers you some built-in Rails help - but all of that is for Rails 2 and does not seem to have been updated for Rails 3. Perhaps we should note that in our install or use instructions. I am really tempted to cheat and just tell KomodoEdit to use emacs key bindings - but that would defeat the purpose of my using the same text editor as our beginning students. So I needed some instructions on how to use it. Found these screencasts but am not yet sure how helpful they are going to be.

Outline

OK so time to start working through the curriculum.

  1. In the Ruby/irb section, make sure to emphasize that irb will echo the output from the last command. Make sure you know if the command you just ran changed the variable you are working with - or just handed you back a new value that is NOT associated with the variable, e.g. talk about the difference between flatten and flatten!.

  2. Discuss how web sites work, requests & responses.

  3. Generate topics scaffold. Walk through it in general terms - database, routes, controllers, views.

  4. Play with generated code in the browser. Suggest adding quiet_assets and thin gems in development block so log output is more sensible.

  5. Add votes resource.
    • Resource is like a scaffold but without the full CRUD support.
    • Add associations. Now a good time to play with “rails console”?
  6. Add button. Then go use it. Ooops, error message. Talk about how to read messages. Mention that Rails shows informative error messages in development mode - and opaque ones in production mode.

  7. Add controller. Discuss each part. Find, build, save!, redirect.

  8. Oooops again. I didn’t read carefully and missed the line about showing the votes. Itterate towards it: display topic.votes, topic.votes.size, add ‘votes’ - but “1 votes” looks dorky, use pluralize.

  9. Commit and Push!

  10. Rearrange page flow after adding new topics. While we are in the application layout page, mention we could spruce things up a bit. Also mention the csrf_meta_tags - built in rails security feature. If we are running ahead of schedule, mention the .json format and AJAX

  11. Simplify topic index page. Discuss link_to. Move delete links to topic details page. Discuss instance variables and loop variables. And how about displaying votes on the show page?

Extras

  1. We didn’t discuss validations any where. Add that for the topics - need a title and description.

  2. Mention the generated tests?

  3. Order topics by vote count.
    • First you need to know the difference between inner and outer joins. And then, the ActiveRecord syntax for each. If you just say “Topic.joins(:votes)”, you get an inner join - so you can never vote for a topic that doesn’t already have a vote. Dho!
      Topic.joins(:votes).group(:topic_id).count(:id)
      SELECT COUNT("topics"."id") AS count_id, topic_id AS topic_id
      FROM "topics" INNER JOIN "votes" ON "votes"."topic_id" = "topics"."id"
      GROUP BY topic_id
       => {1=>2, 2=>5}  
    • The query we want is:
      select topics.*, count(votes.id)
        from topics left outer join votes on topics.id = votes.topic_id
       group by topics.id, topics.title, topics.description; 

      To get that from ActiveRecord, we need to use some SQL fragments along with our other ARel methods - in part so that we can explicitly name the column we want to order by.

      Topic.select("topics.*, COUNT(votes.id) AS count_votes")
      .joins("LEFT OUTER JOIN votes ON topics.id = votes.topic_id")
      .group("topics.id")
      .order("count_votes DESC")
    • Another option is to add a counter cache to Topics and then increment_counter on the topic after each vote is saved (discuss ActiveRecord callbacks). Another option is to just move the votes directly into Topics. Should we? why or why not? Discuss.
  4. Making this site look less ugly. Any front end folks? Talk about the asset pipeline.

Diagrams I need:

  1. How a web site works. Click link in browser -> request -> server -> file or script -> response -> renders in your browser

  2. Our development cycle. Decide what we want -> generate/write code -> play with it locally -> git commit -> push to heroku

Things to suggest for the install notes:

  1. The problem with Xcode command tools and terminfo. Find my backup of the terminfo files and post on this site.

  2. Since we are planning to tell folks about ri in class, add the “complile docs” step to the RVM notes: $ rvm docs generate

  3. How about mentioning how to look at your SQLite3 tables in a GUI? A super simple option being: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  4. What about using RailsInstaller on Mac? Or can we avoid the Xcode mess if we use rubyenv? That helps with Ruby, but what about git? and sqlite3? We can get .dmg files for git. Database? We could use MySQL .dmgs if we like.

  5. Typo in http://curriculum.railsbridge.org/curriculum/CRUD_with_scaffolding?back=creating_a_migration%23step2 Second bullet point under model belongs under view. Add a different line explaining attr_accessible - with warning that many books will not tell you to add fields to that list but you may need to.