Quick Query
December 5th, 2007 -
I was writing some documentation for Rails last night (for Array Conversions), and ran across a handy little method I didn’t know about, which is quite useful.
Say you have a person who has many jobs posted and each one of those jobs have many applicants. You want to see all applicants for all jobs, for a specific person. Here is a really quick example of how to do that:
person = Person.find :first
Applicant.find_all_by_job_id(person.jobs.to_formatted_s(:db))
or, as Chris stated(in the comments), you can just do Applicant.find_all_by_job_id(person.jobs)
That’s that.
Tags: rails
The Rails Creator
December 3rd, 2007 -
A while back I created this gem that took ahold of your hand and walked you through creating a rails application, adding some extra functionality, such as committing it to subversion for you. Well, I never used it – I hated it. It was like something that came out of a bad eighties movie. I mean, a terminal session that talks to you, with text, asking you questions every, and I mean every, step of the way. Sheesh. Annoying! And the code, I won’t even talk about that…
So, it’s time to introduce a new and better version. One I’m actually using now. It still has the same name (thanks drnic), but it has grown up a bit. Let’s see how this thing works:
Say I want to create my rails app using mysql, built on edge rails:
rails_creator -m my_app_name
Say, I don’t want it on edge rails, but rather on the version installed on my machine (with mysql)
rails_creator -M my_app_name
Say, i’m allergic to mysql and want postgres with the rails version on my machine:
rails_creator -P my_app_name
And, what if I’m feeling spicy want to live on the edge with postgres?
rails_creator -p my_app_name
That’s it.
If you want to commit it to one of your svn repo’s, it will ask you after it has created your application. At the time, I wasn’t sure how I wanted to incorporate committing to svn, so it asks you right now ( I know back to the eighties) and gets the location and all that stuff.
Perhaps I’ll update it so you can do rails_creator—m-svn my_app or something like that. Or you can suggest an idea and I’ll take it under consideration.
Want to install it? Just do:
sudo gem install rails_creator
and add some hot sauce. enjoy!
Tags: rails
Rebuild from migrations
November 27th, 2007 -
rake db:reset rebuilds from your schema file, which is great, unless you happen to store some data in your migrations, such as creating an admin account for the system. Then you’d like to rebuild from your migrations. To do so, you’d have to rake db:drop, rake db:create and rake db:migrate. Not a big deal, but in early development, sometimes you find yourself changing previous migration files, etc, so you might be doing this more than once – it can get old.
I also like to make sure that my test db is up to date when I recreate the db from migrations and then I like to annotate my models and fixtures (both in rails default test and rspec). Below is a small and simple snip so that you can do just that by typing rake db:rebuild.
I’m personally using an edited version of annotate_models that will also annotate my rspec models and fixtures. I sent this over to Dave Thomas, but I don’t think he has had the time to add it into his plugin. For my use, I just have it sitting on one of my svn servers.
1 2 3 4 |
namespace :db do desc "Drops and recreates the database from your migrations for the current environment and then annotates your models." task :rebuild => ["db:drop", "db:create", "db:migrate", "db:test:clone", "annotate_models"] end |
Tags: rails
Rails, DHH and a spoonful of Vinegar
October 11th, 2007 -
Not surprising or new, there has been some “new” buzz about Rails, scaling, DHH’s use of the f-word and other irrelevant arguements on why Rails isn’t a good choice for a framework to use and stick with. From what I can tell, it started with this blog post where a lot of assumptions, yes assumptions, are thrown around as truths instead of what they are.
Jamis Buck - The Interview
September 26th, 2007 -
_This has been cross posted at Godbit
Recently, I had the opportunity to sit down with Jamis Buck of 37Signals and Rails Core Team member, and talk about his development, 37Signals, Ruby/Rails, and his personal life. I really had a great time talking with Jamis about various things included (and not) in this interview. I hope you enjoy this interview half as much as I enjoyed doing it!
Tags: rails
Annotate Models
August 10th, 2007 -
When ever I start a new project, I always include the plugin Annotate Models, written by Dave Thomas, from the infamous Pragmatic Programmers.
The plugin saves me time when working with either my models or fixtures, by annotating them with the current schema for that specific model/fixture. It is very useful when in development.
When working with Rspec I really miss seeing the schema for that specific model. So, I wrote a new feature for the plugin that will now annotate both your models and fixtures in rspec, if the directory exists. I’ve sent this off Dave Thomas and he told me he was going to take a look at it and possible include it – when it gets the time to go through it. Safe to say, I was pretty excited to receive a response from the Dave Thomas, considering his stature within the community.
Until then, you can grab it here for download to try out and see if you like it.
Tags: rails
Time Calculation Extension
July 27th, 2007 -
Time.now.end_of_yearTime.now.first_of_the_year (or) Time.now.the_new_yearTime.now.end_of_weekTime.now.next_quarterTime.now.end_of_quarter
I’ve been finding these pretty handy and so I’ve added them to a plugin to extend the Time class. You can grab the plugin here:
http://svn.robertrevans.com/plugins/calc_extension/
Enjoy!
Rails Creator
July 20th, 2007 -
Previously it was called, creator. Today, it has a new name: rails_creator. Dr. Nic suggested the idea to make it easier to remember and if you forgot, you could type rails and hit tab to see it pop up in the Terminal window.
You can either download the gem at or you can open up your terminal/command line and type:
gem install rails_creator
Go check it out and use it to create your rails applications on edge or not and check-in/check-out from subversion.
Permalize Fun
July 1st, 2007 -
I was playing around with my permalize plugin to optimize it just a bit. Initially it was around 8 lines of code that did the actual parsing of the string. After a few improvements, it is now a one liner. Man I love Ruby!
(Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
Creator - Rapid Rails Setup
June 17th, 2007 -
Finally, this weekend I found some free time to finish off my first gem release: Creator – Rapid Rails Setup.
I’ve been promising this gem for some time now, but with numerous projects going on at work, several freelance projects, and trying to find time to spend with my 1 year old little girl, it’s been hard to find time to do any personal projects these days. (phew, now that is a sentence!) Since it was father’s day, I decided I would do a few things I have been wanting to do for months now, and this was one of them.
I have a Ruby Forge account setup for the project, just head over here and grab the gem. Once you have it installed on your system, open a terminal window and type creator. There are instructions that will walk you through the process allowing several options:
- Create your project on Edge Rails or whatever version you have installed on your system.
- Choose what database you’re using (mysql, postgres, etc) – this sets up your config/database.yml file
- Choose the name of your Project
- Decide if you are checking this into subversion or not
- If you are checking it into subversion, then it will automatically check it out again on your local machine – this because it will then do svn:ignore on several files (database.yml, log, tmp)
Hopefully in the near future I’ll have some time to add a few more options, such as not checking it out from subversion, after checkin, of you so choose.
If you have any suggestions of what you’d like to see added, just leave me a comment. Hope you like it!
IBM & Rails Caching
May 30th, 2007 -
This is a bit late, but I was pretty thrilled to see a bunch of links coming from IBM’s Developer Works site. Bruce Tate provided my rails caching as a reference to his excellent article on Rails Caching.
He covers the basics of rails caching: page, action and fragment caching and also goes into a bit on memcaching and model based caching. It’s a good read and I highly suggest that you check it out.
Tags: rails
Rcov
May 23rd, 2007 -
I posted this over on my Ruby Snips site.
I really like using rcov for my testing. It helps show those little places in your code that could use more tests. However, it gets annoying to keep writing out the rcov test/unit/* rcov test/functional/*. Perhaps there is a better way to run those that is quicker, I’m not sure, but I wrote a little rake task to take care of it for me. Here it is:
1 2 3 4 5 6 7 8 9 |
namespace :test do desc "Run rcov for both unit and functional tests." task :rcov => :environment do return unless %w[development test staging].include? RAILS_ENV `rcov test/unit/*` `rcov test/functional/*` `open -a firefox #[RAILS_ROOT}/coverage/index.html` end end |
That “open -a” line will open up firefox to the index.html page that was just created so you can view your test coverage. It is OSX specific. To for windows, just use start firefox and I assume you can pass the url as I’ve done above, but I’m not quite sure – I haven’t tested it.
Permalize
April 15th, 2007 -
It’s been quite a while since last I blogged. I’ve been really busy with work and freelance – some of which I’ll talk about in a later post. For now, I wanted to introduce my first plugin for Rails, Permalize.
I had gone through the Rails source looking at the various methods that extend the String class in Ruby and didn’t find any that would work well as a permanent link creator. There’s dasherize (which I thought would work, but it is for underscores to dashes), pluralize, titleize and a bunch of other goodies. Thus, permalize was born.
Rapid Rails setup - revisited
February 8th, 2007 -
I’ve taken some time to cleanup my set_rails bash script and have made many changes to it. One thing I’ve come to dislike about the script is passing the variables (app name, database, svn) when I am executing the script. I tend to forget which order I had them set in, thus making a script that was meant to quickly setup a Rails app without thinking, into having to think about it and, at times, open up the script to see what order the variables go in. Perhaps you’ve had the sample problem using the script?
Another aspect I dislike is having the svn check-in and check-out split into two scripts and that are ran at different times. Makes no sense, period. It all should be done within one full swoop.
Tags: development, rails
Textmate Snippets
January 25th, 2007 -
I love Textmate. It is by far the greatest text editor available. Any other editor currently on the market cannot compare to the simplicity and yet vastness of Textmate.
What I really like about it is that you can create code snippets to speed up your productivity when coding. I feel that are a few missing in the HTML and Rails bundles. So, I’ve created a few that I use often and thought others might like to use what I have created.
Tags: development, rails, ruby