Sword 1.0b5 · A simple and friendly weblogging tool for academic environments created by Fingertips design & development.

 
 
 
 
 

Rails ActiveRecord bug

TrackBack link: http://blog.markjuh.net/markjuh/trackback/2005/7/29/rails_activerecord_bug

Published on 29 July 2005 at 16:43, updated at 16:48.

In category Rails.

Today I had to make sure a model couldn’t be saved if someone tried to update a certain field.

Reading in the API documentation I found the following:

If a before_* callback returns false, all the later callbacks and the associated action are cancelled.

So a before_update callback in my model would be the solution. I just had to make sure it would return false in the right cases.

Because of the last three months of test-driven development, I decided to write a test first. And to my surprise I could always save the model, even if my before_update callback would always return false. First I double-checked my test, but it was okay. Time to use yet another great tool in Rails: script/console.

It showed that even though the save method always returned true, it in fact did not save the model. So fortunately no problem with the before_update callback. This meant that there was no way avoiding it anymore… I would take my first dive into the ActiveRecord code.

After quite some time of searching back and forth through a couple of Ruby files, I found the problem:


def create_or_update
  if new_record? then create else update end
  true
end

And see here, it always returns true instead of the actual result from create or update. Why? Well, probably because there would be a problem in the create method:


def create
  self.id = connection.insert(
    "INSERT INTO #{self.class.table_name} " +
    "(#{quoted_column_names.join(', ')}) " +
    "VALUES(#{attributes_with_quotes.values.join(', ')})",
    "#{self.class.name} Create",
    self.class.primary_key, self.id, self.class.sequence_name
  )     

  @new_record = false 
end   

This create method returns the value of new_record (which is false), instead of the result of the query (self.id).

I’ve written a simple patch (see ticket #1861), which solves my problems and doesn’t introduce any new problems in my application. But again, it would be good to have a test for ActiveRecord. The problem is that I’m just not well-acquainted with ActiveRecord, so I have no idea where or how to incorporate the test. Any help with this would be greatly appreciated.

Sword

TrackBack link: http://blog.markjuh.net/markjuh/trackback/2005/7/27/sword

Published on 27 July 2005 at 22:30.

In category Sword.

This blog has now moved to my own machine and it’s running Sword.

Since most people will not be interested in every note I make about this project, I’ve moved the frequent and detailed posts about Sword to it’s own blog. It will mostly contain posts about new versions and features.

This blog will contain things I’ve uncovered during Rails development. Interesting behaviour or pieces of Rails code that might be of interest.

Back up again

TrackBack link: http://blog.markjuh.net/markjuh/trackback/2005/7/5/back_up_again

Published on 5 July 2005 at 14:14, updated on 25 July 2005 at 15:27.

In category TextDrive.

Finally I’ve been able to get this blog up and running again after somehow going down and not being able to start up again.

I checked the lighttpd.conf file, but I couldn’t find anything that was wrong. But after throwing away the configuration file and setting it up again from scratch, it now works again!