ActiveRecord saving madness?

Hello,

First of all, I love RoR. I am studying it, and I am finally “getting
there”.
However, I do have a bit of a problem with ActiveRecord’s saving
behaviour.

Imagine to have Orders (id, name) {has_one:invoice} and Invoices (id,
order_id) {belongs_to :order} .
If you type:

invoice = Invoice.new(…whatever…)
an_order.invoice = invoice

if invoice fails to save, you will never know. What’s worse, an_order
will be saved, and will have a nasty nil, which means that your rhtml
template will fail when going through that record.

You have to do this instead:

invoice = Invoice.new

fill in the invoice

invoice.save!
an_order.invoice = invoice

As far as 1:0|1 specific “issues”, if you do:

an_invoice = Invoice.new(…whatever…)
an_order.invoice = an_invoice

an_invoice will be automatically saved (why…?)

Finally, in order to keep this a 1:1 relationship, if you type:

invoice=new(…whatever…)
order.invoice=invoice

The “old” order.invoice will be modified, so that it will effectively be
an orphan (order_id is set to null).

Now… I have two questions:

  1. Why doesn’t ActiveRecords have 1:1 relationships by sharing the
    primary key, as people would normally do? Or… does it?

  2. Who (and I mean: WHO) would ever use the “feature” of children
    records being saved automatically, if s/he can’t know if the operation
    worked or not?!?

This is really driving me bananas. For the first time, I sit here and
think: maybe it’s true what P.H. says; maybe no magic at all is better
than too much magic :expressionless:

(Confused)

Bye,

Merc.

Hi,

Hummmm any takers on this one?

Merc.


Tony M. wrote:

Hello,

First of all, I love RoR. I am studying it, and I am finally “getting
there”.
However, I do have a bit of a problem with ActiveRecord’s saving
behaviour.

Imagine to have Orders (id, name) {has_one:invoice} and Invoices (id,
order_id) {belongs_to :order} .
If you type:

invoice = Invoice.new(…whatever…)
an_order.invoice = invoice

if invoice fails to save, you will never know. What’s worse, an_order
will be saved, and will have a nasty nil, which means that your rhtml
template will fail when going through that record.

You have to do this instead:

invoice = Invoice.new

fill in the invoice

invoice.save!
an_order.invoice = invoice

As far as 1:0|1 specific “issues”, if you do:

an_invoice = Invoice.new(…whatever…)
an_order.invoice = an_invoice

an_invoice will be automatically saved (why…?)

Finally, in order to keep this a 1:1 relationship, if you type:

invoice=new(…whatever…)
order.invoice=invoice

The “old” order.invoice will be modified, so that it will effectively be
an orphan (order_id is set to null).

Now… I have two questions:

  1. Why doesn’t ActiveRecords have 1:1 relationships by sharing the
    primary key, as people would normally do? Or… does it?

  2. Who (and I mean: WHO) would ever use the “feature” of children
    records being saved automatically, if s/he can’t know if the operation
    worked or not?!?

This is really driving me bananas. For the first time, I sit here and
think: maybe it’s true what P.H. says; maybe no magic at all is better
than too much magic :expressionless:

(Confused)

Bye,

Merc.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs