Archive for the 'Ruby on Rails' Category

Posted on September 28, 2006 at 10:25 am

Update_attributes is not very smart; it will update a record whether or not the hash you pass it contains any changed values. If you aren’t careful, you could end up with a ton of database writes for no reason. Try my update_attributes_if_changed method instead:

module ActiveRecord
  class Base

    def update_attributes_if_changed(hash)
      update_attributes(hash) if needs_update?(hash)
    end

    def needs_update?(hash)
      hash.stringify_keys!
      !attributes.dup.delete_if{|k,v| !hash.key?(k) || hash[k] == v}.empty?
    end

    def write_attributes(hash)
      attributes.merge(hash.stringify_keys)
    end

  end
end

I’ve thrown in a method called write_attributes too. It works just like update_attributes, but it doesn’t automatically save the record.

Posted on September 26, 2006 at 4:43 pm

Yesterday, somebody asked me if it’s possible to format the contents of an input field using the text_field form helper.

I don’t think its documented in the API docs, but you can specify the value to use by passing in a value parameter:

text_field :book, :title, :value => @book.title.upcase
Posted on July 28, 2006 at 5:56 pm

I’ve been working on a very large Rails projects at work and found myself spending a lot of time writing the same unit tests over and over again. The application has a very complex database schema and has 159 ActiveRecord models so far. With this many models, there are obviously a ton of associations. I decided that I needed a way to automatically test every association that is defined in a model.

The TestInjector plugin is the result. I still have a lot of functionality that I want to add, but it works very well and it’s ready for others to use.

I posted the details on the Rails wiki:
http://wiki.rubyonrails.org/rails/pages/TestInjector

Or just install it using the plugin manager and take a look at the README file for more details:

script/plugin install http://www.infused.org/svn/plugins/test_injector

or:

script/plugin discover
script/plugin install test_injector
Posted on July 20, 2006 at 12:09 pm

Anybody else have this problem with Anna Chan’s ScopedAccess plugin?

Using a around_filter with ScopedAccess::Filter or the scoped_access helper works fine in normal usage, but when running rake test:functionals, the scope from previously run tests carries over into later tests. In other words with_scope is being set but not removed.

Maybe this is because the controllers are not being torn down after the corresponding functional test is run? I’m tired of peppering my functional test setup methods with Whatever.reset_scope. Anybody have a quick fix for me? I have other things to do, and I’d like to avoid looking deeper into this one…

Posted on June 15, 2006 at 2:09 pm

I’ve been keeping myself user busy the last few weeks and haven’t made the time to post anything.

  • RailsConf 2006 is only 5 days away. I’m flying. While the ArgonExpress would be a good opportunity to hang out with the cool kids, I can’t stomach the thought of being stuck in on a train for several days. I hope they make it to Chicago alive.
  • The server migration is almost complete. Last night I installed lighttpd on the new box, setup Apache to proxy requests to the Rails sites running under lighttpd, and migrated all of the Subversion repositories. There is only one site left to move. Just waiting on the client.
  • I’ve written a lot of stuff in Rails over the last several weeks which may be useful to others. I’ll try to package these up and post them before or during RailsConf:
    • Global unit tests: Tests that are defined in one place that will run for any ActiveRecord model with the right attributes. So far I have test_acts_as_versioned, test_optimisitic_locking, and test_fixtures_loaded_for_all_associations. The last one is very handy. It reflects on all associations and gives you a list of fixtures that need to be defined in each unit test.
    • Plugin: update_attributes_if_changed. Only updates the attributes if anything has changed. If you are using update_attributes on models with updated_at columns and acts_as_versioned you’ll want this.
    • ActiveRecord synchronizer: One way synchronization with data transformations, conditions, defaults, etc. Very handy for synchronizing with legacy databases. Complete with DSL.
    • Datadumper plugin: Dump data in several format, but mosty used for dumping data into a format usable in migrations. Especially nice for prepopulating domain tables using migrations.
    • Validation: validates_association. Not to be confused with validates_associated. Validates_association validates that the associated object is of the correct class as specified in the association declaration.
  • Surprisingly, I found out that I really like watching soccer.