:has_one association not letting me delete parent record

Hi all,

I have two models Order and Transaction witht he following

has_one :transaction
belongs_to :order

and there's an order_id column in the transactions table. These models
have various associations with other models too.

When i try to delete an order though, it does not get deleted. When i
comment out the association line in the model 'has_one :transaction',
the delete works fine. Its puzzling! There are no before_destroy
callbacks on either model yet the log shows this line

SELECT * FROM `transactions` WHERE (`transactions`.order_id = 80)

this is the last line in the log. The order is not deleted and neither
is the transaction. Any idea what im doing wrong?
oh and i tried adding :dependent => :destroy on the :has_one too. That
dint work either.

Anyone? ideas? i really am not able to figure this out.. nothing in
the API docs, google is not being its dependable self..

Whats interesting is that if there is no transaction for a particular
@order, then @order.destroy fails. But if there IS a transaction for
the given @order, then @order.destroy returns true but the order is
still there. It doesnt get deleted!! WEIRD!

And the logs carry the same queries.. no difference.
I have another has_one association on the same Order model but that
doesnt give me any of this pain.. i just dont get it..

Have you tried running the (failed) delete sql manually in phpmyadmin
or whatever to see if it works then?


Hi Colin,

It never gets to the delete sql at all. There is no delete sql in the
log. There is a 'select' on transaction which i cant figure out how.

the log o/p

  Parameters: {"action"=>"destroy", "_method"=>"delete",
"id"=>"80", "controller"=>"orders"}
  e[4;36;1mOrder Columns (0.003615)e[0m e[0;1mSHOW FIELDS FROM
  e[4;35;1mOrder Load (0.002041)e[0m e[0mSELECT `orders`.* FROM
`orders` INNER JOIN users ON orders.user_id = users.id WHERE
(`orders`.`id` = 80) e[0m
  e[4;36;1mTransaction Load (0.001054)e[0m e[0;1mSELECT * FROM
`transactions` WHERE (`transactions`.order_id = 80) LIMIT 1e[0m
Redirected to http://localhost:3000/orders

What ive figured out so far is that @order.destroy returns whatever
"SELECT * FROM `transactions` WHERE (`transactions`.order_id = 80)
LIMIT 1" returns.
My model definitions couldnt possibly be simpler. Its as ive defined
them above.. just cant figure it out.. any ideas at all? doesnt seem
to be any ticket on it in Lighthouse.. could it?.. indeed.. be.. a...
Im gonna try and install the query trace plugin and trace where that
query is coming from...
any other ideas?

The query comes from


New Relic's agent. hmm.. maybe not a good idea to have 'transaction'
as a model name?

If i do

Order has_one :txn, :class_name => "Transaction"

things work just fine.
But i still dont get how the line in New Relic's agent generates that
Gonna let them know..

http://wiki.rubyonrails.org/rails/pages/ReservedWords lists
Transaction as a reserved word in RoR, I wonder if having a model
called that is the root of the problem.


Hmmm.. I removed the new relic agent and still had the problem. The
query on the transactions table seems to come from orders_controller
where I call @order.destroy.

Again, if i change the association name and define the class_name as
"Transaction", then it works. Its interesting that although its a
keyword, its enough to change just the association name to get it to
work. the model is still Transaction.

Strange things can happen when using reserved words. If that is the
problem then surely something else will go wrong as you write more
code. It would be easy enough to test, change all Transaction to Txn,
all transaction to txn, rename the model and controller files the view
template and the view folder and test files plus anything else I have
forgotten and try it.


:slight_smile: done already. Works fine.