Hi all, 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.
Anyone? ideas? i really am not able to figure this out.. nothing in the API docs, google is not being its dependable self.. On Jun 27, 11:58 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > 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.. On Jun 29, 11:25 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Anyone? ideas? i really am not able to figure this out.. nothing in > the API docs, google is not being its dependable self.. > > On Jun 27, 11:58 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all, > > > 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.
Colin Law
2009-Jun-30 07:27 UTC
Re: :has_one association not letting me delete parent record
2009/6/29 Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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? Colin
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", "authenticity_token"=>"2c84880c410eab07f8ed9125520c50e440eb3105", "id"=>"80", "controller"=>"orders"} ... [4;36;1mOrder Columns (0.003615) [0m [0;1mSHOW FIELDS FROM `orders` [0m [4;35;1mOrder Load (0.002041) [0m [0mSELECT `orders`.* FROM `orders` INNER JOIN users ON orders.user_id = users.id WHERE (`orders`.`id` = 80) [0m [4;36;1mTransaction Load (0.001054) [0m [0;1mSELECT * FROM `transactions` WHERE (`transactions`.order_id = 80) LIMIT 1 [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? On Jun 30, 12:27 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/6/29 Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > 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? > > Colin
The query comes from vendor/plugins/newrelic_rpm/lib/new_relic/agent/instrumentation/ active_record_instrumentation.rb:40:in `log_without_trace_Database___adapter_name____args_1__'' New Relic''s agent. hmm.. maybe not a good idea to have ''transaction'' as a model name? On Jun 30, 12:39 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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", > "authenticity_token"=>"2c84880c410eab07f8ed9125520c50e440eb3105", > "id"=>"80", "controller"=>"orders"} > ... > [4;36;1mOrder Columns (0.003615) [0m [0;1mSHOW FIELDS FROM > `orders` [0m > [4;35;1mOrder Load (0.002041) [0m [0mSELECT `orders`.* FROM > `orders` INNER JOIN users ON orders.user_id = users.id WHERE > (`orders`.`id` = 80) [0m > [4;36;1mTransaction Load (0.001054) [0m [0;1mSELECT * FROM > `transactions` WHERE (`transactions`.order_id = 80) LIMIT 1 [0m > Redirected tohttp://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? > > On Jun 30, 12:27 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > 2009/6/29 Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > 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? > > > Colin
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 query.. Gonna let them know.. On Jun 30, 12:58 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The query comes from > > vendor/plugins/newrelic_rpm/lib/new_relic/agent/instrumentation/ > active_record_instrumentation.rb:40:in > `log_without_trace_Database___adapter_name____args_1__'' > > New Relic''s agent. hmm.. maybe not a good idea to have ''transaction'' > as a model name? > > On Jun 30, 12:39 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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", > > "authenticity_token"=>"2c84880c410eab07f8ed9125520c50e440eb3105", > > "id"=>"80", "controller"=>"orders"} > > ... > > [4;36;1mOrder Columns (0.003615) [0m [0;1mSHOW FIELDS FROM > > `orders` [0m > > [4;35;1mOrder Load (0.002041) [0m [0mSELECT `orders`.* FROM > > `orders` INNER JOIN users ON orders.user_id = users.id WHERE > > (`orders`.`id` = 80) [0m > > [4;36;1mTransaction Load (0.001054) [0m [0;1mSELECT * FROM > > `transactions` WHERE (`transactions`.order_id = 80) LIMIT 1 [0m > > Redirected tohttp://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? > > > On Jun 30, 12:27 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > 2009/6/29 Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > 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? > > > > Colin
Colin Law
2009-Jun-30 08:42 UTC
Re: :has_one association not letting me delete parent record
2009/6/30 Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > If i do > > Order has_one :txn, :class_name => "Transaction"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. Colin
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. On Jun 30, 1:42 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/6/30 Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > If i do > > > Order has_one :txn, :class_name => "Transaction" > > http://wiki.rubyonrails.org/rails/pages/ReservedWordslists > Transaction as a reserved word in RoR, I wonder if having a model > called that is the root of the problem. > > Colin
Colin Law
2009-Jun-30 11:06 UTC
Re: :has_one association not letting me delete parent record
2009/6/30 Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > 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. Colin
:) done already. Works fine. On Jun 30, 4:06 pm, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/6/30 Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > 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. > > Colin