Posts Tagged ‘Rails’
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: destroy_all, patch, Rails
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: activerecord, case, Rails, Ruby
Posted in Rails, Ruby | Comments (1)
2008090808_moving_to_denver.rb September 8th, 2008
The lack of posting was not done in vein. Over the past few weeks I have been interviewing with companies and finally settled on Factory Design Labs. This will be a nice move away from the Java world that I have been seemingly trapped inside for the past few months into a Rails position. Since I am in Chicago and the new position is in Denver, that means its time to move and to what a better city! I am an avid fan of everything outdoors and living close to the elements. This post is just a status update for anyone contemplating removing my blog from RSS for lack of updates. I will begin posting regularly again after my first few days at Factory Labs on September 23.
Tags: chicago, denver, factory labs, Java, Rails
Posted in Miscellaneous | Comments (0)
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: help, Merb, Rails, Ruby, rubyme, tutor
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:
[code language="shell"]sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart[/code]
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: .htaccess, mod_rails, passenger, permalinks, Rails, Ruby, wordpress
Posted in Rails, Ruby | Comments (1)
Living on the edge with Merb July 17th, 2008
If you have ever had the urge to live life on the edge of software development, the project you should be following right now is Merb. By now if anyone has been attending or watching the ruby community podcasts then you have heard this software mentioned. The reason I suggest merb is not because I am a huge promoter of the framework (yes, some may call it my flavor of the week) but it is just so exciting to be apart of its development.
Merb has much a growing community around it that it is hard to believe suggestions are still so welcome and encouraged. I personally have weighed by two cents on a few topics in the past week and they seem to have been taken into account. To top it all off, the lead developer Ezra Zygmuntowicz has time to answer personal questions via IRC. I doubt I would ever see DHH do this for any Rails newbie.
Anyway, its time for me to get back to checking out this elegant little framework.
Merb IRC: irc.freenode.net:6667 #merb
Tags: edge, ezra zygmuntowicz, Merb, Rails
Posted in Merb | Comments (0)


