rubycoloredglasses


I'm Jason, a web applications developer in the Denver, Colorado area.


  1. Finding Records without Specific Child in Many-to-Many Relationship

    Okay. Here is a tricky challenge. Let’s say you are coding a blog system where Posts may have many Tags, and a tag can have many posts. Your database would have a ‘posts’ table, a ‘tags’ table, and a ‘post_tags’. With Ruby on Rails this would be configured for an ActiveRecord model using the has_many through method.

    has_many :tags, :through => :post_tags
    

    I only want posts which have an absence of a relationship with a specific record, which is the tag record representing ‘horrible’. How do I query for a list of posts which are absolutely without a specific tag? Like say I have a ‘horrible’ tag, and I want all posts which are not tagged with ‘horrible’. How would I accomplish this?


  2. Listing Gems from Rails Console

    Got this from Stack Overflow, figured it could come in handy at some point in the future.

    Gem.loaded_specs.values.map {|x| "#{x.name} #{x.version}"}
    

  3. Add a Serialized Hash Attribute to a Factory_Girl Definition

    I recently declared an ActiveRecord model which stores a serialized Hash inside of a text field. When I tried to setup a factory for this model using FactoryGirl, I received many syntax errors. This is because FactoryGirl attributes expect a single value or a certain form of code block.

    factory :post do
        title        "Example Post"
        body         "This is the body of the example post"
        meta         { "version" => 2 }
        created_at   "2012-06-01 17:53:13"
      end
    

    To include a hash as an attribute of a factory, declare the Hash separately and then simply assign it directly in the factory definition.


  4. List Sorted Methods in Ruby

    I often use ‘methods’ to get a list of methods available for an object in Ruby, but it can be a pain trying to look through the list for what I want. I wish it outputed in a sorted list straight down the page. This template will help you achieve that. Maybe I should override the ‘methods’ method. Hm…

    "object".methods.sort.each do |method| puts method end
    

    If you want to get only methods with a certain string inside them, use this:

    "object".methods.sort.each do |method| puts method if method.to_s.index('search_string') end
    

  5. Updating a Serialized Object from a Web form

    You may run into a situation where you create some sort of standard Ruby class that you want to associate with an ActiveRecord model. The serialize method allows you to store an object inside of a text field for an ActiveRecord object.

    With Rails 2.3 support for models nested within forms was added, but it’s clear that this support isn’t compatible with serialized objects. In my example below I have a Post model, which represents a simple blog post. The serialized object is instantiated from a custom class called ‘Metadata’, which stores metadata for the post like it’s type and version.


  6. RSpec Controller Tests Receiving 'No route matches' Error

    I’m developing a Rails engine gem for the company I’m working for, which will provide an API for the applications we’re using. The gem I’m creating will be used with a Rails 3.0.9 system, using Rspec-Rails version 2.10.1. I had a route to my API interface setup in the config/routes.rb file like so:

    Rails.application.routes.draw do
      match '/companyname/api_name' => 'CompanyName/ApiName/ControllerName#apimethod'
    end
    

    When I added a ‘get’ request call to my controller test, I was getting this error:

    Failure/Error: get :apimethod
    ActionController::RoutingError:
      No route matches {:controller=>"company_name/api_name/controller_name", :action=>"apimethod"}
    

  7. Cubase Installation Failure

    I recently ran into issues installing Cubase 4 on my Mac running Snow Leopard. I uninstalled Cubase 5 Essential, thinking that this was causing a conflict, and thus stopping me from installing an older version. This wasn’t the case I tried to install Cubase 5 Essential again, and I got the same type of error with it’s installer. I received Cubase 6 in the mail today and tried to install it…only to receive the same type of error:


  8. Generators Not Working in Rails 2.3.8

    I’m currently working on a gem that is going to use a generator to create files in a Rails 2.3.8 application. One of the applications we’re still using is using Rails 2.3.8, so I have to make a gem compatible with that version of Rails.

    I installed Bundler v1.0.22 and configured it to work with the Rails app, and then followed many instructions and various configurations to get my generator classes to load. Every time I would try to run the generator however it simply gave me the error “Couldn’t find ‘hello’ generator”.


  9. Establishing New Ruby Environment in a Folder using RVM

    I know this is documented on the official RVM website, but I hate having to look it up over and over again each time I want to create a new RVMRC file.

    $ mkdir -p ~/projects/rails2test
    $ cd ~/projects/rails2test
    $ rvm --rvmrc --create 1.8.7@rails2test
    $ cd ..
    $ cd rails2test
    ==============================================================================
    = NOTICE                                                                     =
    ==============================================================================
    = RVM has encountered a new or modified .rvmrc file in the current directory =
    = This is a shell script and therefore may contain any shell commands.       =
    =                                                                            =
    = Examine the contents of this file carefully to be sure the contents are    =
    = safe before trusting it! ( Choose v[iew] below to view the contents )      =
    ==============================================================================
    Do you wish to trust this .rvmrc file? (/Users/jmiller/Documents/rails2-apps/.rvmrc)
    y[es], n[o], v[iew], c[ancel]> y
    
    

  10. History of the Canonical Gem Host for Ruby Gems

    The default repository for downloading gems using RubyGems was originally RubyForge.org. This was likely because the RubyGems project was hosted only from RubyForge. This meant that when you ran ‘gem install rails’, your RubyGems installation was configured to download the gem from ‘gems.rubyforge.org’. In August of 2008 Github started gaining popularity amongst the Ruby community after it started providing it’s own gem server via gems.github.com. This resulted in many Ruby developers configuring RubyGems to use the Github server as a secondary source of gems.

    In August of 2009 a new gem hosting repository came onto the scene known as GemCutter, aiming to resolve issues caused by how Github and RubyForge were handling hosting of gems. In September they decided to move the service to RubyGems.org. In October of Github discontinued building and serving gems from gems.github.com, and RubyGems.org became the official default host for Ruby gems.