Tag: Ruby on Rails

Force Rails Version When Creating a New App

I have multiple versions of the Rails gem installed on my development server. We’ve been using Rails long enough now that we have many apps created for many versions. As I make changes to each app I need to test them in their specific version (the version listed in the environment.rb file) so having multiple Rails versions installed is a necessity.

But if I try to spin up a new application using

$rails new_app

I get a rails app created specifically for the latest version of Rails installed.

But what if you need to create a new app for an older version of rails that is also installed? The following easy command does the trick:

$rails _1.2.3_ new_app

Where 1.2.3 is the desired version of Rails I want new_app to run. Note the underscores before and after the version.

Bookmark and Share

No Response  |  add yours »

Titleize an Array List in Rails

First, a trick. If you want to display a comma separated list from an array all you need to do is multiply that array by a comma. “What’s that”, you say? You heard right. If you have an array in the variable @my_array you can simply place the following in your view:

<%= @my_array * ", " %>

The comma and space in your string will be added to the end of every string in your array except for the last one. Beautiful! I love rails.

Now for the reason for my post. While I’ve known this little trick for a while, I came across the need to titleize the outcome because my array contained all lowercase strings and I wanted to diplay them as titles.

It took a little trial and error, but the solution is rather simple:

<%= (@my_array * ", ").titleize %>

To apply the titleize method, you simply wrap your multiplication is parenthesis and add the .titleize as you normally would.

(Note: I still have not upgraded to Rails 2.0, so I cannot guarantee any of this will work in newer versions)

Bookmark and Share

2 Responses  |  add yours »

Rails Controller Variables not Loading

Don’t forget: “render :layout => …” should appear below any variables in your controller.

I’ve spent a few hours trying to get the @variables set in my controller to show up in my layout. After much frustration and trying all kinds of fancy ways around my problem, it suddenly occurred to me that I was simply loading the layout before the variables; I was loading them in the wrong order! Grrr…..

If you are having problems with variables not loading properly, check the order you call the render. It might save you a headache and the need for as much Pepsi as I went through trying to figure this stupid thing out.

Bookmark and Share

2 Responses  |  add yours »

How I converted Mephisto to WordPress

I got a comment the other day asking how I converted to WordPress from Mephisto while keeping my comments and everything intact. If you are familiar with Ruby on Rails, this response will make sense. If you are not, I recommend learning it…then this response will make sense.

In a nutshell, I used Mephisto for the conversion. First I spent about a week mapping Mephisto’s DB to WordPress’ DB. I went straight into the Mephisto Rails project and created a new model that pointed to my newly created WordPress database. I then created a controller in Mephisto that looked exactly like this:

(note: if this is a pain to read, you can also view it here)

class MephistoController < ApplicationController
    def index
        start_time = Time.now
        logger.info 'querying mephisto articles'
        @articles = Content.find(:all, :conditions=>"article_id IS NULL")
                logger.info 'querying mephisto comments'
                @comments = Content.find(
			:all, :conditions=>"article_id IS NOT NULL")
        logger.info 'querying mephisto taggings'
        @taggings = Tagging.find(:all)
        logger.info 'querying mephisto tags'
        @tags = Tag.find(:all)
        logger.info 'processing terms'
        for tag in @tags
            @wp_term = WpTerm.new
            @wp_term.term_id = tag.id
            @wp_term.name = tag.name
            @wp_term.slug = tag.name.downcase.gsub(" ","_")
            @wp_term.term_group = 0
            @wp_term.save
        end
        logger.info 'processing term relationships'
        for tagging in @taggings
            @wp_tr = WpTermRelationships.new
            @wp_tr.term_taxonomy_id = tagging.tag_id
            @wp_tr.object_id = tagging.taggable_id
            @wp_tr.save
        end
        logger.info 'processing term taxonomy'
        for tag in @tags
            c = Tagging.count(:all, :conditions=>"tag_id = #{tag.id}")
            @wp_tt = WpTermTaxonomy.new
            @wp_tt.term_taxonomy_id = tag.id
            @wp_tt.term_id = tag.id
            @wp_tt.taxonomy = "post_tag"
            @wp_tt.parent = 0
            @wp_tt.count = c
            @wp_tt.save
        end
        logger.info 'processing posts'
        for article in @articles
            c = Content.count(:all, :conditions=>"article_id = #{article.id}")
            @wp_post = WpPost.new
            @wp_post.ID = article.id
            @wp_post.post_author = 1
            @wp_post.post_date = article.published_at-7.hours
            @wp_post.post_date_gmt = article.published_at
            @wp_post.post_content = article.body
            @wp_post.post_title = article.title
            @wp_post.post_category = 39
            @wp_post.post_status = "publish"
            @wp_post.post_status = "open"
            @wp_post.ping_status = "closed"
            @wp_post.post_name = article.permalink
            @wp_post.post_modified = article.updated_at-7.hours
            @wp_post.post_modified_gmt = article.updated_at
            @wp_post.post_parent = 0
            @wp_post.guid = article.published_at.strftime("http://blog.gillumiante.com/%Y/%m/%d/")+article.permalink
            @wp_post.menu_order = 0
            @wp_post.post_type = "post"
            @wp_post.comment_count = c
            @wp_post.save
        end
        logger.info 'processing comments'
                for comment in @comments
            @wp_com = WpComment.new
            @wp_com.comment_ID = comment.id
            @wp_com.comment_post_ID = comment.article_id
            @wp_com.comment_author = comment.author
            @wp_com.comment_author_email = comment.author_email
            if comment.author_url == nil
                comment.author_url = ""
            end
            @wp_com.comment_author_url = comment.author_url
            @wp_com.comment_author_IP = comment.author_ip
            @wp_com.comment_date = comment.published_at-7.hours
            @wp_com.comment_date_gmt = comment.published_at
            @wp_com.comment_content = comment.body
            @wp_com.comment_karma = 0
            @wp_com.comment_approved = '1'
            @wp_com.comment_parent = 0
            @wp_com.user_id = 0
            @wp_com.save
                end
        logger.info 'finished!'
        end_time = Time.now
        @lapsed = end_time-start_time
        render :layout=>false
    end
end

I then proceeded to visit the /mephisto/index page on my mephisto blog, which fired this baby off. It took all of about 9 seconds to complete.

I realize the irony of using Mephisto in order to abandon it. But in the end, I wasn’t switching from the Rails based app because of Rails, but because of the app. I am still in love with rails and as you can see, this project would have taken me a lot longer to accomplish had I attempted to write it in PHP, the language of WordPress.

Bookmark and Share

4 Responses  |  add yours »

Mephisto to Typo migration?

This blog you are reading is powered by Mephisto. I originally created this blog using my own code in PHP and was so tired of re-inventing the wheel that I went out and researched what I thought would be the best blogging software for my needs. I’m in love with Ruby on Rails, so one of my requirements was that it had to be using that technology. In my research I narrowed it down to Typo and Mephisto, but there wasn’t much activity over in the Typo camp and there were plenty of people blogging about their switch from Mephisto to Typo. So, I chose Mephisto thinking it was the better of the two.

I’ve since realized that Typo’s development activity has gone way up, and Mephisto’s has gone way down. I have been using Typo on another blog venture of mine and am in love with it. Actually, I’m loving it much more than Mephisto these days for several reasons, most of which pertain to user friendliness. I did run into an issue with trackback spam on Typo, which I’ve read as a big complaint about it. But trackbacks aren’t even an option on Mephisto, so what’s the big deal?

The point I’m trying to make here is this. If there’s anyone out there with experience migrating from Mephisto to Typo I’d like to hear from you. There’s plenty of google hits about going from Typo to Mephisto, but nothing in the opposite direction. I’m interested in converting this blog, Tongue and Groove (once again).

Bookmark and Share

2 Responses  |  add yours »

Search