Thursday, January 26, 2006

Transactions

A number of my actions create objects of different models with some sort of association which are then saved in the database. To ensure data integrity, all the resulting SQL statements mustbe carried out, which basically means a transaction is needed.

Looking at the rails API though gave me a bit of a shock:


If you have multiple class-specific databases, the transaction will not protect interaction among them. One workaround is to begin a transaction on each class whose models you alter:

Student.transaction do
Course.transaction do
course.enroll(student)
student.units += course.units
end
end

This is a poor solution, but full distributed transactions are beyond the scope of Active Record


Thoughts about how my skills are not up to this task started to rise as I foresaw the corruption of clients data. I dug around a bit though and came up with the following, which also tidies up my code a bit:


def old_signup
@company = Company.new(@params[:company])
@user = User.new(@params[:user])
@address = Address.new(@params[:address])
@user.admin = true
@user.company = @company
@user.address = @address
if @request.post? and @user.valid? and @company.valid? and @address.valid?
@user.save
@company.save
@address.save
@company.addresses << @address
flash[:notice] = 'Signup successful'
redirect_to :action => 'welcome', :user => @user
else
flash.now[:notice] = 'Signup failed'
end
end

def new_signup
@user = User.new(@params[:user])
@user.admin = true
@user.company = Company.new(@params[:company])
@user.address = Address.new(@params[:address])
if @request.post? and @user.valid?
User.transaction do
@user.save
# return
@user.company.addresses << @user.address
end
flash[:notice] = 'Signup successful'
redirect_to :action => 'welcome', :user => @user
else
flash.now[:notice] = 'Signup failed'
end
end



I've tested it and it works; if 'return is uncommented, then the database is not altered. The only problem is that only the user is now validated. Hmmm.

Friday, January 20, 2006

Salted Hash Login

While I've been developing planlist/planr/pantracker (still haven't decided on a name yet) I've been aware that my login system is lacking somewhat. I decided to grapple the bull by the horns last week, installed the Salted Hash Login Generator! and tried to incorporate it into my app. SHLG has email support for forgotten passwords and account confirmation as well enabling passwords to be altered (a problem I've been struggling with), not to mention built in localization.
I dived into the code and just hit wall after wall. I'm sure it's really beautifully written code but I found it incredibly difficult to follow what was going on. This all started to give me doubts about my idea, my abilities and so on.
And then last night I had another moment of clarity - keep it simple. I don't need to be as good a hack as the guy who wrote SHLG. So I'm going to take the bits that I need and incorporate them into my login model. Bosh.

Friday, January 06, 2006

I love Rails

It dawned on me last night that the pagination, tag and company tag code in my templates are all the same for overview, sendout and manage_tags. So all three templates call the partials _tags, _company_tags, and _pages now. Partials are such a good idea. I'm going to pull the code in the actions into a helper now (or place it in the models?).

Thursday, January 05, 2006

It's a long long road

... with many a winding turn. I really thought I was near finishing the developement phase but that's not the case. I'm asymptotically approaching it. Spent a bit of time putting together a basic css layout so it's not such an eyesore to look at. Got pagination working on overview. Just need to add it to dispatch and manage_tags. Thought I'd got the edit password problem sorted until I tried to log in, couldn't, and then saw in the DB that my password field for my login was empty. hmmm. Anyway, I need to take the GTD approach and write down on cards what still needs doing, ticking them off when they're done otherwise I'll never finish.

Tossed and turned last night in bed thinking it's not a good enough idea to bring in the cash. Envisioned having to stay at work until I retire. Or getting thrown onto the streets and ending up being like Jack Nicholson at the beginning/end of The Pledge, tormented by a near-miss.

Convinced again though that it is a good idea!