Posts Tagged ‘tip’
I’ve been getting in and out of rails development for some time now… and my biggest problem is finding documentation for simple things (I guess that people already know all the stuff, but I’m still getting up to speed).
So I decided to blog about one of those simple things… in case there is still someone who has not mastered Ruby on Rails.
When several models are saved in a single transaction in Rails, the usual case is that you want to rollback the transaction if any of them fails. This is done automatically if an exception is thrown.
The example in ActiveRecord::Transactions::ClassMethods accomplishes this by using save! on the models.
However, you usually also want to display validation errors and not show the full rails trace to the user.
What the example does not show is how to accomplish this. Two options here:
- use save (no exclamation mark) and check the return value. If any of the saves return false, raise an ActiveRecord::RollbackException after your render or redirect.
- rescue from ActiveRecord::RecordInvalid and render or redirect there.
I think #2 is more elegant… but since I knew about it too late, my code uses #1.
begin
transaction do
first.save!
second.save!
third.save!
fourth.save!
end
rescue ActiveRecord::RecordInvalid => invalid
# do whatever you wish to warn the user, or log something
end
Example is from this other blog post. I wish I’ve read it sooner!
This one is probably for newbies.
When providing a SelectionListener for doing a something when a SWT button is pressed, you’ll need to implement two methods:
Button b = new Button(container, SWT.PUSH);
b.addSelectionListener( new SelectionListener()
{
public void widgetSelected( SelectionEvent e )
{
// TODO Auto-generated method stub
}
public void widgetDefaultSelected( SelectionEvent e )
{
// TODO Auto-generated method stub
}
} );
However, only one of those methods will be called: widgetSelected
So you can save a couple of lines (which is always nice, specially when inlining the handlers like in these examples) by using SelectionAdapter, an implementation of SelectionListener with default implementation of both methods (which is to do nothing).
The final, shorter, core looks like this:
Button b = new Button(container, SWT.PUSH);
b.addSelectionListener( new SelectionAdapter()
{
public void widgetSelected( SelectionEvent e )
{
doSomething();
}
} );
