I'm trying to delete an account but at the same time i'd like to
delete the portals that belongs to the account.
This is what i wrote but it's not working.
how can i deal with this problem?
I have this code inside the account controller in the "destroy" method
def destroy
@account = Account.find(params[:id])
@portal = Portal.find(:all, :conditions => ['account_id = ?',
@account.id ])
@portal.destroy@account.destroy
end
any ideas?
the error displays this:
NoMethodError in AccountsController#destroy
undefined method `destroy' for #<Array:0x47982a0>
app/controllers/accounts_controller.rb:78:in `destroy'
Request
returns an array, even if there is only one result. Check this out:
User.find(:all, :conditions => "id = 1").class
=> Array
Only one result is returned but it is wrapped in an array. And class
Array does not have a destroy method, hence the error. One simple way
around this would be to do:
I'm trying to delete an account but at the same time i'd like to
delete the portals that belongs to the account.
This is what i wrote but it's not working.
how can i deal with this problem?
Why not specify it in the models?
class Portal < ActiveRecord::Base
belongs_to :account
end
class Account < ActiveRecord::Base
has_many :portals, :dependent => :destroy
end
Use :dependent=>:destroy only if you need to do some 'clean up'
processing in the child models. Destroy instantiates each of the
models before deleting them. If you don't need to do any processing
then use the more db-efficient :dependent=:delete_all