rubycoloredglasses


I'm Jason, a web application developer in East Tennessee.


  1. Locate and Updatedb with Homebrew

    UPDATE: I ran into errors and decided to not use the findutils provided by Homebrew.

    When you run the locate command, the system will now tell you to run the service that creates the file database:

    WARNING: The locate database (/var/db/locate.database) does not exist.
    To create the database, run the following command:
    
      sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
    
    Please be aware that the database can take some time to generate; once
    the database has been created, this message will no longer appear.
    

    You’re better of simply running this and then using the built in ‘locate’ command.


    I used to use MacPorts to ensure that my command line environment on my Mac was almost exclusively using MacPort provided binaries, not the built in binaries and libraries that are packaged with Mac OS X.


  2. Foreign Key References when Generating Model

    I forget the proper syntax for a model generation command that includes a reference to another models id (foreign key).

    Here is an example you can use to remember:

    rails g model Post user:references title:string body:text
    

    Since the ‘user’ model already exists, Rails knows that this should be the user_id field that it generates. I guess it’s not a big deal, you could just do ‘user_id:integer’, but what fun is that?


  3. Edit Devise User without Password

    I recently setup a custom controller to edit/update my Admin accounts, which are authenticated using Plataformatec’s Devise gem.

    I found an article in the Devise Wiki that mentions using some sort of ‘update_without_password’ method to update the model without requiring the password. In this case I’m not requiring the user to provide their own password to edit their info. I’m allowing them to do it straight out.

    The solution that I found was to simply remove the ‘password’ and ‘password_confirmation’ from the parameter set if both are blank.

    # remove password parameters if blank
    if params[:admin]['password'].blank? && params[:admin]['confirmation'].blank?
      params[:admin].delete('password')
      params[:admin].delete('password_confirmation')
    end
    

  4. Factory Girl Not Generating Factories with Scaffold

    I just started a new Rails 3.2 project, and to ensure that the proper test files are generated using Shoulda or Factory_Girl, I’ve installed those gems and configured the application to generate the test files using these gems.

    Added to config/application.rb:

      # Configure generators values.
      # http://guides.rubyonrails.org/generators.html
      config.generators do |g|
        g.stylesheets false
        g.test_framework :shoulda
        g.fallbacks[:shoulda] = :test_unit
        g.fixture_replacement :factory_girl
      end
    

    Each time I would try to create a new scaffold, it would use shoulda to generate the test unit file, but would generate a YAML fixture.


  5. Ruby Comparison Operator =~

    I saw this in some code recently, wasn’t sure what it did.

    It basically returns TRUE or FALSE if there is a regular expression match, with the regular expression coming after the ‘=~’.


  6. Invalid Gemspec Error Regarding Invalid Date Format

    I installed factory_girl (2.6.0) for a project I am working on recently, and all of a sudden I started getting errors with RubyGems when I would try to run a rake task, such as:

    Invalid gemspec in
    [/opt/local/lib/ruby/gems/1.8/specifications/capistrano-2.11.2.gemspec]:
    invalid date format in specification: "2012-02-22 00:00:00.000000000Z"
    
    Invalid gemspec in
    [/opt/local/lib/ruby/gems/1.8/specifications/capistrano-2.9.0.gemspec]:
    invalid date format in specification: "2011-09-24 00:00:00.000000000Z"
    

    I went through the long process of uninstalling all my gems, running an update on RubyGems using ‘gem update –system’ and then reinstalling them again via ‘bundle install’. Still the gems were uninstalled, yet the specification errors were still occurring.


  7. Deleting Git Branches in Remote Repository

    I had recently used a branch to handle all the modifications I was making to a system for a Rails 3.1 upgrade from Rails 2.4.3. After I merged my changes back into the master branch, I deleted the ‘rails3’ branch locally, but it still remained on the remote server.

    I found that ‘git push origin :branch_name’ will delete the repository from the remote server if the branch has been removed locally.

    $ git push origin :rails3
    To git@example.com:myrepo.git
     - [deleted]         rails3
    

  8. Rails 3 on WHM / cPanel VPS Server

    cPanel is working towards making Rails 3 applications run natively with Passenger, setup via the cPanel interface. I’m not really sure if this will be ideal, as most organizations deploy their apps to the server using Capistrano, not uploading via FTP or something.

    I’ve been hosting a number of PHP driven sites, including this blog, from a shared hosting service for quite a while now. Shared hosting is fine for personal websites or even small businesses with 4-5 page brochure style websites that do not receive lots of traffic, but they’re not fine if slow performance or intermittent downtime causes you to loose business (or even the respect of your visitors). In such cases I recommend a VPS, because you control who you’re hosting and thus can ensure optimal uptime and performance. I highly recommend Linode as a VPS provider.


  9. Configuring Rails 3.1.3 under Sub-URI

    In setting up a new Rails app recently I was told that it needed to be served under the sub-URI of ‘/info’. I hadn’t done this before with a Rails app, and I expected that it could be tricky.

    I checked online to see how this is done and found references to the ‘relative_url_root’ setting, but then shortly found that this has been deprecated.

    I’m using Phusion Passenger with Apache 2, so I inserted the following configuration into my VirtualHost entry for the site.


  10. Custom Rake Tasks Not Loading

    I recently went to create a new Rake task under /lib/tasks in a Rails application I’m working on. I didn’t understand why the rake tasks weren’t showing when I would run rake -T from the command line.

    When I’d try to run the task itself I would get a ‘Don’t know how to build task’ error.

    I just realized that I was naming my task file with the ‘.rb’ extension, and not ‘.rake’. Doh!