Design question - errors and exceptions

Hi,
I hope someone can help, this is driving me mad.

I’m adding different postage methods to my cart which are user
selectable.
A cart can have only one postage method, and a postage method can have
many rates. So, I have this:

class Cart < ActiveRecord::Base
belongs_to :postage_method
end

class PostageMethod < ActiveRecord::Base
has_one :cart
has_many :rates, :class_name => ‘PostageRate’
end

class PostageRate < ActiveRecord::Base
belongs_to :postage_method
end

What I’m doing is updating the cart postage cost attribute every time
the the cart is updated (add item, remove item, etc). This is based on
the total weight in the cart. I have data like this for each postage
method.

lower upper price
0 100 1.25
101 200 1.50
201 300 1.75

So, my questions.

  1. Should I dump the database table and put this in a non ActiveRecord
    class?
    I don’t know if I’m over complicating this when I know there will
    probably only ever be two postage methods.

  2. If the user chooses the method which has a limit, and the cart is
    over that limit, I was going to automatically change the users postage
    method and recalculate their postage using the new postage method. I
    need to show the user a message explaining this. The problem is, how
    to I propagate that message up through two different objects to the
    controller? Do I throw a PostageChanged exception and then catch it in
    my controller? The problem with doing that is that my database update
    is then rolled back because I am throwing an exception within an
    ActiveRecord model.

Is there a way to get active record to not rollback on an exception
thrown?

How would you handle this sort of problem?

Any advice would be appreciated.

Cheers,
Jordan

On Apr 21, 2008, at 6:28 PM, Jordan E. wrote:

belongs_to :postage_method
end

class PostageMethod < ActiveRecord::Base
has_one :cart
has_many :rates, :class_name => ‘PostageRate’
end

class PostageRate < ActiveRecord::Base
belongs_to :postage_method
end

Looks like the cart ought to own the postage method, not the other way
around. That should make your other issues much simpler.

Hi,

Looks like the cart ought to own the postage method, not the other way
around. That should make your other issues much simpler.

Thanks, but how would that make my issues simpler?

How do I send a signal to the controller to show what has happened?
Anyone?

Thank you :slight_smile: