This is one of those things that is probably obvious to some, and just not known to some. I was in that latter description up until a few minutes ago. It’s that you can easily validate any belongs_to relationship by simply using valdiates_presence_of.
class Comment < ActiveRecord::Base
belongs_to :article
validates_presence_of :article
end
If you instantiate a new Comment, and don’t assign an Article to the comment instance, the record won’t save. I have to say, that seemed pretty obvious. But there is another interesting piece to this.
Say I passed the article_id in the params and tried to assign that to our instance
comment = Comment.new
comment.article_id = params[:article_id]
If article_id corresponds to a real article in our database, then comment.article will have be assigned an Article with that id@. Now if @params[:article_id] happens to be some id which doesn’t exist, like 99999999999, then comment.article will be nil and thus the record fails validation.
This means that you don’t have to test for the existence of the Article against your records because rails will do this for you when it checks to see if comment.article is nil or not.
In what I can think of as the most important JavaScript that I have ever written, I have made something which will parse the DOM and replace all of the ‘l’s with ‘r’s. This idea came about after watching Team America: World Police about 1,000 times and after Kim Jong Il gave his apology for doing some nuke testing. Back to the script:
function engrish(n) {
if(n.nodeType == document.TEXT_NODE) {
n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');
} else if(n.hasChildNodes()) {
for(var i=0; i<n.childNodes.length; i++) {
engrish(n.childNodes[i]);
}
}
}
To execute this code, simply call engrish(document.documentElement);. Using recursion, this function goes through the DOM, finds text nodes and replaces the characters.
Read the rest of this entry »
This is a fairly simple little tidbit, but still useful. Sometimes I want to have a script for my rails application, like running some reports on the data. There are two basic ways to do this: write a task(rake), or create a new ruby script file, like in the scripts directory.
If you want to do the latter, then there are two lines you my want to put at the top of your script.
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
The first line allows us to supply which environment we would like to run the script in. Say the script is called process.rb, we could call the script like ./process.rb production and now the environment is set to production.
The second line loads the environment file. Now all of rails, your models, libraries, and plugins are available to you.
I love migrations. I think they are one of the best things about rails. I love that I can count on any changes I’ve done in the database to be consistent across all other databases that I use. Love it.
Sometimes I find that I want pre-load my database with some data, like categories that I have. For testing purposes, I have already created this data in my fixture; now I just want to load that fixture into my database from a migration call.
I know that there is a rake task for this, so I looked into that code and came up with a little something.
Read the rest of this entry »
As a rails user who loves migrations, I have noticed that all of the migrations are prefixed with three decimal places, like 001_create_sessions.rb or something of that nature. That got me thinking, what would happen after I had 999 migrations?
That sets of the paranoia level up because who knows, I may need 1001 migrations! So to settle this once and for all, I wrote a script that would generate 999 migrations for me. I ran the 1000 migration and then it created 1001_test1001.rb.
Good news! Rails doesn’t constrain its migrations to a 3 decimal place prefix. So pointless…
I love TextMate. I love being able to load in a directory as a project, like I do with a lot of my rails applications. When I’m using Finder to navigate my filesystem, I sometimes want to open a directory into TextMate from Finder, and a contextual menu would be perfect.
Using some knowledge of Automator, this can be done quite easily.
Read the rest of this entry »
I desperately want to use edge rails to have all the goodies of Rails 1.2, especially the RESTful stuff. The problem is that it’s bleeding edge and I am working on a project that will be public and not for my own desires.
There is a simply_restful plugin available, which is what Rails 1.2 will be modeled after. The problem is that there will be differences between the plugin and the rails source. That means after using one, I have to refactor some code to include conventions like change my delete methods to destroy and some pluralizing stuff as well.
That’s where the simply_restful_backport plugin comes in. The author has made a plugin which implements the RESTful stuff as it will be in Rails 1.2. I am going to try it out this weekend and see how she fits.
So today I had an interesting problem. For whatever the reason, I wanted to have a class automagically be created. It was going to be a helper model, and I didn’t feel like having umteen classes that all did the same thing, just had different names for different tables. My solution: use the power of ruby and metaprogramming.
def create_new_active_record_model(class_name)
klass = Class.new(ActiveRecord::Base) do
belongs_to :old, :class_name => class_name
def some_method_you_want
"something"
end
end
Object.const_set "#{class_name}Target", klass
end
I know that some of this could have been encapsulated in a polymorphic association, but this allows you to do something beyond just keeping everything in one table, like with polymorphic associations.
Read the rest of this entry »
Beast is a nice, light-weight forum written in Rails. From what I can gather, the overall goal is to make something work with only 500 lines of code, and I think that could happen with this. It is open source, and can easily be checked out.
I did check it out and I have to say that I’m impressed. It is using the rails edge and is trying out some of the RESTful stuff all the while adhering to the philosophies of CRUD. I have wanted to look into a good example of a rails project using REST, and this code base is great for that. What’s more is that it’s not very difficult to wrap your head around all of the code of the site, due to the small code base and nice abstraction of the code.
Read the rest of this entry »
Google has released another beta product called Google Image Labeler. The premise is you and a random partner have 90 seconds to get a match on as many pictures as you can. A match is a label or tag that you give to the image is the same as one your partner has given. So it’s a little game you can play in your off-time, and if you sign in, you can use a display name.
This is just genius. By creating a game out of it, Google is having the users of the internet help them find better ways for users to search for images. The other brilliant part is that it only lasts 90 seconds, so you are forced to think quick, and thus you have more of a stream of conscious going on and you are less likly to filter your response.