Feel the ease of time tracking with NR Time. Log your time expenses in the billing-friendly format with minimal effort.
Posted: May 18th, 2010 | Filed under: Programming, Tips | Tags: rails, ruby | No Comments »
Assuming that you have an object Account like below
class Account < ActiveRecord::Base
has_one :office
end
and you want to create an Office record when you create an account, you could do it in the after_create filter and it would be my way of doing that also. But the funny thing is that if you name the filter create_office, you don’t need to define the method itself. It will create the Office and link it to your Account model automagically. So here’s how the final class definition looks like:
class Account < ActiveRecord::Base
has_one :office
after_create :create_office
end
Amazing!
Posted: August 6th, 2009 | Filed under: Programming | Tags: edge, rails | No Comments »
Can’t help to share my joy. If you did this (see below) prior to current Rails Edge version when your time zone in environment.rb is set to anything but “UTF-8″, you did it wrong.
class Model < ActiveRecord::Base
named_scope ..., :conditions => { :date_time_field => Time.now }
end
Why? Because the Time.now will be taken in your server local time zone (set in environment.rb) and compared against the UTC saved in the database. To do it right, you needed this:
The same was applicable to finders and other stuff taking dates as arguments. Not very nice. I personally wrote patches to fix this in my projects.
Finally, in the latest Rails Edge it was fixed! No more kludge. Taken from the official post:
What time is it!?
Geoff Buesing provided a useful fix for Time, specifically when used in conjunction with ActiveRecord. Now you can save and search ActiveRecord objects using whichever local time zone you like, regardless of what your default time zone is configured for and everything now will just work. You no longer need to be concerned about converting your user’s local time into your default application time or vice versa.
Posted: July 27th, 2009 | Filed under: Programming | Tags: gems, plugins, rails | No Comments »
To keep up with the quickly evolving Rails community I constantly read many blogs and watch podcasts. Here are some curious new gems I discovered recently.
Rack::Bug. A comprehensive debugging plugin for your Rails applications that sits on the Rack middleware level and captures a ton of curious info ranging from request parameters and Rails environment to the request processing timings, SQL execution details and much more. It’s not suitable for the older Rails-based projects as it needs the Rack which has become the part of Rails (or Rails was adapted to run in the Rack stack to be precise) just recently, but for new projects it can be a life saver.
FiveRuns TuneUp. While still in beta, this plugin and their server-side service may give a good quick insight into what’s going on under the hood of the request processing. It sits at the top of the page as a bar and gives a nice JavaScript popup with the response rendering tree and model schemas. I noticed a some quirks and wasn’t able to dig deep enough to nail a couple of problems I noticed, but it’s a great general-purpose tool to quickly see who’s the greediest of them all.
NewRelic RPM. Not new, but for the completeness sake. A great paid (with a free plan) service with the app-side agent plugin for performance data collection. Collects ALL possible data about your app down to the execution plans of your statements and error trace-backs, but costs a fortune. None of my clients went higher than a Bronze plan because of their cosmic prices. I’m sure they have their customers, it’s just not for everyone. Still, thanks for NewRelic guys (honest thanks), there’s a way to get access to all info if you have a head, hands and some time.
Metric_fu. All-in-one code analysis package grouping several other tools and providing a unified web interface with graphs and other niceties. Not much more to say here. Useful as hell if you on the quality side of the application development business.
Whenever. Quoting the author, “Whenever is a Ruby gem that provides a clear syntax for defining cron jobs”. This is one of an awesome piece of software. Rare Rails app these days needs no background / periodical processing. Even simple news e-mail delivery is done with cron + rake or similar solutions. This tool lets you keep your cron jobs definitions in ruby and with a tiny bit of an effort, you can add the crontab regeneration to your deployment process. No need to handle all these crontabs manually any more.
That’s all folks. However, this brings up an interesting question. How do you discover what new tools / gems emerge in the Rails realm these days? It develops insanely rapidly.
Posted: June 1st, 2008 | Filed under: Programming | Tags: mod_rails, passenger, rails, ubuntu | No Comments »
My gosh, I can’t believe I missed this. On May 21 Brightbox has announced a happy marriage of a revolutionary mod_rails (Passenger) project from Phusion to the excellent user-friendly Linux for the mere mortals — Ubuntu.
It means that the deployment and management of Rails applications becomes a breeze. I personally still prefer my very custom deployment setup — Nginx-Mongrel (for Rails) + Nginx-Apache (for PHP), but for those planning to unroll massive Rails hosting, it may be a famous Red Pill.
Love Rails!
Posted: June 1st, 2008 | Filed under: Programming | Tags: mocha, rails, rails 2.1, shoulda | No Comments »
3 hours ago Rails 2.1 was finally released into the wild, and there’s the officials report for your reading pleasure. I’m still a bit concerned about the RSpec compatibility, but since I already found a sufficient replacement — Shoulda and Mocha — there may not be as many worries.
So, let’s give it a round of aplause. Great job!
Posted: January 29th, 2008 | Filed under: Programming | Tags: constant, Programming, rails, ruby, tip, uninitialized | 7 Comments »
Last two weeks I was seeing the production server of one of my clients hit the same error over and over with no visible pattern. This second it works, the next it fails and leaves the Mongrel cluster in an undefined state somewhere between this world and hell.
The error I’m referring to is:
NameError (uninitialized constant MyModule::Utils::NetSession)
... backtrace follows ...
I need to repeat that it comes and goes under some non-obvious circumstances, and is hard to recreate. Fortunately, I found the solution and here’s what it was.
I have a directory hierarchy like below. I’m sure when you see it, it all becomes obvious and simple, but it wasn’t for me at that time as there were many files that I omitted below.
/lib
/my_module
klass1.rb
...
utils.rb
/utils
net_session.rb
The piece of code in klass1 that failed looks as follows:
def new_session
Utils::NetSession.new
end
Simple, heh.
Utils.rb is a helper module which is included into MyModule, but apparently loaded only when really necessary (it’s the only explanation why it didn’t fail all the time, as you will see). When it is loaded and included, the MyModule::Utils becomes defined, and Utils::NetSession resolution starts to fail. It happens as Rails begins looking for Utils name space from MyModule, finds MyModule::Utils and doesn’t find any NetSession there. That’s pretty much all to it.
The solutions are:
- Either rename utils.rb (which I did in favor of utilities.rb), or
- Use “::Utils::NetSession.new” (which is not safe as one day you can forget about the leading “::”, and get back to roots)
Hope this helps someone like me fighting the way through these extremely infrequent but painful Rails gotchas.
Posted: October 30th, 2007 | Filed under: Programming | Tags: haml, Programming, rails, ruby, sass, staticmatic | 3 Comments »
Hey fans of everything concise. My finding of today is Haml and Sass which is a quickly growing replacement for Rhtml (and plain HTML) and CSS. Just check the syntax sample on their main page and admit it, it’s much simpler and cleaner than plain (R)HTML. Everything has a meaning, everything cuts to chase.
Haml can be used with Ruby on Rails as a plug-in that transparently adds the support to your ActiveViews. You simply rename your views from .rhtml to .haml and start deleting redundant code. In case of CSS, you place your .sass in the stylesheets folder and they are automatically compiled on demand. One thing I haven’t figured out yet is whether they do every time, or once per change to save performance.
Haml and Sass are also used in the StaticMatic toolkit, which is exactly how I discovered them. The toolkit lets you put together static sites lighting fast. You simply write your pages in Haml, model your stylesheets in Sass, preview and do a final build. As the result of building you get a bunch of little cute HTML files and CSS stylesheets ready for upload to the site. If you, like me, still like static sites and want to save time on bringing them up and maintaining, it’s your choice.
Let me know what you think. A couple of lines is fine.