Archive for the ‘Rails’ Category

destroy_all with conditions

November 3rd, 2008

In a has_many relationship, there should be a way to add conditions to destroy in order to eliminate an explicit find.


@post.comments.find(:all, :conditions => "name LIKE 'stupid%'").each {|record| record.destroy }

With a patch I recently submitted to Rails core, you can now trim this line into:


@post.comments.destroy_all "name LIKE 'stupid%'"

Jump on over to Lighthouse bugtracker and try out the patch.

Tags: , ,
Posted in Development, Rails | Comments (0)

Ruby’s and Rails’ case gotcha when comparing classes

October 15th, 2008

While pairing yesterday I ran into quite an interesting problem when using the Ruby case statement. Now this should not come to a surprise because the case statement uses the === operator rather than the == operator (that is common in other languages). We were refactoring some code like this.


variable = group_class == Animal ? 8 : 2

initially this turned into


if group_class == Animal
  variable = 8
elsif group_class == Water
  variable = 2
else
  variable = nil
end

This code was simply meant to check against class type since group_class is holding the type of this object. We decided to change this into a case statement as we could see more group_class types coming down the road in the next iteration.


variable = case group_class
               when Animal: 3
               when Water: 8
               ....

So this code was assumingly supposed to work, however does not. Apparently the valid way of writing this class is to do:


variable = case group_class.name
               when "Animal": 3
               when "Water": 8
               ....

Then this code works as designed. Tracking down the reason for this is ActiveRecord’s overloaded == and === operators.


# File activerecord/lib/active_record/base.rb, line 1269
1269:       def ===(object)
1270:         object.is_a?(self)
1271:       end
-----
# File activerecord/lib/active_record/base.rb, line 2421
2421:       def ==(comparison_object)
2422:         comparison_object.equal?(self) ||
2423:           (comparison_object.instance_of?(self.class) &&
2424:             comparison_object.id == id &&
2425:             !comparison_object.new_record?)
2426:       end

Just a quirk I noticed. Enjoy :)

Tags: , , ,
Posted in Rails, Ruby | Comments (1)

TextMate reigns supreme with ‘Ack in Project’

September 29th, 2008

The one thing holding TextMate back from Ruby / Rails domination is its horribly slow search functionality. Using ‘Search in Project’ will take what seems like an eternity to pop up any results. The only real attempt at solving this headache was to use ‘Grep in Project’, but it left a lot to be desired including search speed. Now comes the solution, ‘Ack in Project’ was a project created on GitHub back in August to use the much more efficient Ack library for searching. With enhanced output, it has given other IDEs like NetBeans and IntelliJ a swift quick to the balls as to what is the best Ruby / Rails editor.


$ cd /Applications/TextMate.app/Contents/SharedSupport/Bundles/
$ sudo git clone git://github.com/protocool/ack-tmbundle.git Ack.tmbundle

Use Ack in Project with Cmd+Shift+A

Tags: ,
Posted in Development, Rails, Ruby | Comments (18)

Rubyme opens its doors.

August 20th, 2008

I have been working long hours getting this site up and running. The concept was initially drafted after some tutoring work I had done and the difficulty I had in coordinating everything. If you have not yet figured out, the site is called Rubyme (www.rubyme.net). The goal of the site is to provide an easy way for users to find help on anything from specific project difficulties to a more typical tutor style. Currently many of the features are still under heavy development, but the basic functionality is there. I encourage you to sign up and become a tutor. It is a great way to make some money, get exposure, and learn. There will be ongoing patches, so please report any problems to my email address: justin.smestad@gmail.com

Tags: , , , , ,
Posted in Development, JRuby, Merb, Rails, Ruby | Comments (0)

mod_rails setup issues and solution

August 20th, 2008

So I was migrating my server over to Debian Lenny from Etch and ran into some interesting problems along the way. The first of which is related to an open ticket on the mod_rails issue tracker. The conflict appears to be with Wordpress permalinks, .htaccess and mod_rails. However, without my knowing when I installed mod_rails (a.k.a Passenger) via the script it disabled by rewrite module. So when I went back to check this blog, running wordpress, all the permalinks were broken. I had tried everything from setting ‘RailsAutoDetect off’ and ‘RailsAllowModRewrite on’, however neither of these are even needed. I just had to issue the simple commands:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

After these two commands, you can stop pulling out your hair and begin shaking your fist at the sneaky passenger/mod_rails installer. Just goes to show that automated installers are not always the holy grail.

Tags: , , , , , ,
Posted in Rails, Ruby | Comments (1)