I have two models Order and Transaction witht he following
relationship.
Order
has_one :transaction
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)
LIMIT 1
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.
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..
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",
"authenticity_token"=>"2c84880c410eab07f8ed9125520c50e440eb3105",
"id"=>"80", "controller"=>"orders"}
...
e[4;36;1mOrder Columns (0.003615)e[0m e[0;1mSHOW FIELDS FROM
`orders`e[0m
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...
BUG!!?!?
Im gonna try and install the query trace plugin and trace where that
query is coming from...
any other ideas?
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.