Wednesday, May 20, 2009

select field with default value in rails

I wanted to make a select box from a list in the database, and I wanted the default value for a field to be the person logged in. Should be simple and it was, but the api didn't have any example, so here is what I used.
I'm using form_for so my code was different than the docs too, maybe this is in there somewhere..
<% form_for(@task) do |f| %>
<%= f.label :user_id %>
<%= f.collection_select(:user_id, User.find(:all), :id, :name, {:selected => session[:user_id]})

If there is a better way let me know.

Tuesday, May 19, 2009

Rails - Dealing with nil in views

I've been working on the task management application and after creating my initial databases and views started to make the UI more friendly. Each task in the application has an owner. I'm doing this with two tables, one called tasks and one called users. The task table has a reference to the user called user_id. When you pull the task index view you end up seeing the ID reference instead of the person's name. That's not too helpful.
My first attempt was to put code like this in the view:

in view/index.html.erb


Owner:
<%=h User.find(@task.user_id).name %>



The problem is when the user_id doesn't have a value or the value is not valid. Lots of ways to solve this, like add a validation when a task is created that says it exists, but what if you want to make the task but don't know who should own it yet? And what if you delete a person, because they leave the company. Need some routine to reassign their tasks. But in any case, index shouldn't crash.
My first thought, and I think a bad one was to put exception logic in the view. As soon as I started typing, I knew that was bad.
So I came up with this simple solution in the model:

in model/task.rb
def owner
begin
User.find(self.user_id).name
rescue
"None"
end
end

Regardless of the reason for the crash its caught and index will keep working. It doesn't depend on other routines to make sure this piece of code doesn't fail. Best of all, you could write a test on this too. Not a big deal here, but if it got more complex you might want it.
The view is much more readable like this:
<%=h @task.owner %>

One last thing I think I should do is log something in the exception. That assumes someone would notice a problem in the log, but its not a bad practice.

If you have a better way to resolve this, let me know.

Saturday, May 16, 2009

New Project

I've tried basecamp and actionmethod and these are pretty good tools. They are free and you should check them out. I use Microsoft project every day. Its not a good tool. I hate to say that, but its just a pain. There are other tools for sprinting all that, which are neat. If you have a good one, let me know, but either way, I'm writing a new one. We aren't selling tool, but it needs to do some things we want to do - basic stuff.
We want to track tasks, but we also need to report on tasks for the project. Actionmethod didn't have the team reporting we wanted. We also need to track the time spent on each item. The time reporting key for us. Basecamp had most of the features we wanted, but didn't have the time reporting. We looked at time reporting tools, but they were focused on tracking time and reporting it, but not really the task management.
The final straw was when using MS Project I tried to sort a plan by start date. Couldn't make it work in MS Project 2003. We have groups of tasks, that have summary tasks and a few columns of data. The report features in MS Project kept pushing the print out to two pages side by side. That is too much of a pain too. But the big deal was the sort was actually wrong. The summary tasks kept creating problems.
We just wanted an easy way to log in and see, what work do I have to do today. Then for the manager, what work got done this week and what is not getting done. So we can communicate with clients who might be impacted. Of course we need to bill hours so we need to track the time and we want to be able to analyze the time to make better estimates next time. Sounds so simple.
If you have a great tool for this, let me know. For now I'm building one in Ruby on Rails, I'll let you know how it goes.

Wednesday, May 13, 2009

Upgrading Ubuntu 8.10 to 9.04

I finally pressed the button. It kept calling to me day after day, upgrade available. Finally I pushed it. Everything was running great in 8.10, so I'm not sure what I really hoped to gain, but two things that I wanted were OpenOffice 3.0 and better dual monitor support. Things I could probably live with out, but I did it anyway.
It really did seem to go well. After the reboot the system came up, just like I expected and I was off and running. I quickly installed CouchDb using apt-get which I'm not sure was available in 8.10. It might be, but its not in 8.04. That's another story.
I went to use Skype today and no mic. ouch. I haven't found the fixes yet, but when I do, I'll try to post them here.
Other than that, seemed to be so painless, I hardly noticed the change.
If you read this, let me know your favorite features in 9.04.

Saturday, May 9, 2009

Pentaho Kettle doesn't cut it - update

Admittedly I'm a Pentaho newbie, trying to evaluate the software and to some 'simple' tasks. The demo's and samples worked well, but the put the test Kettle just didn't add enough value as an ETL to warrant its use. To be fair, I have used WebMethods and some other tools but have usually found it more simple to just write the code. There always seems to be something unique enough that the tool just can't handle where writting the code work, or worst yet a defect that you can't work around.
In the case of Kettle, I just have a problem I can't work around. Its open source so in theory I can debug the original code and create a patch. Unfortunately I was trying Kettle because I thought it would be simple and I was in a hurry. So I don't have time to debug at that level, since writing the code might take as long as writing the blog entry. Because I didn't buy the Pentaho version, I don't have a support option.
Here is what I wanted to do. Open a file that included a single string that was compressed with gzip. That string contained XML and I wanted to put that XML into a database. Seemed simple. I used the load file and gzip decompress, did a preview of my decrypted string and seemed to look good. I had to sent it to Javascript to make a modification to the XML before I could parse the XML. That is where the trouble started. The resulting string was not whole, it had parts missing. I couldn't figure out why, but Kettle is truncating my string at 4600 char when it really has closer 8600. I tried setting the length of the field to 10000, but that did not work. I gave it 4 hrs of debugging and some time on IRC but there is no obvious solution yet.
Reading the 'documentation', I use that term loosely, did not help with this problem. In fact even for open source documentation, there were a lot of issues. I understand that the wiki is going through some changes, so stuff is moving around in an attempt to make the documentation better. Clearly the code is moving faster than the documentation. So for now I'm going to post the issue to the forum, and write my own because I have a job that needs this done!

Update:
I wanted to update this post based on what I did to resolve the issues I was having. I did join the IRC Channel, which I would recommend, but that did not solve the problem. The string problem so far as I can tell is truly there. What I did to solve my particular challenge is make the make the XML well formed via a bash script before loading it into kettle and then use the XML input functions. That worked like a champ in under 5 minutes. That said Kettle was easily able to deal with the XML and create the output I wanted.
Looking forward I will continue to work with the Pentaho toolset. Hopefully the work on the documentation by community will continue to pay off for the tool. I have for my part tried to update the Pentaho wiki when I could. I will continue to do that when I can, or post what I can here. This is not a simple project to document, it changes quickly and getting started is considerable undertaking.
To summarize, I'd say if you are working on a straight forward task the tool performed well. The debug features and the documentation are two areas that require improvement in order to be commercial class tools in my opinion. The debug features and improvements would probably be the most helpful if I had to prioritize them.