我有一张桌子,说人,如果我做的话
person = Person.find(:first)
person.delete
我得到了
NoMethodError: undefined method `to_sym' for nil:NilClass
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1403:in `block in expand_hash_conditions_for_aggregates'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1402:in `each'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1402:in `expand_hash_conditions_for_aggregates'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/relation/query_methods.rb:261:in `build_where'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/relation/query_methods.rb:124:in `where'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/relation.rb:380:in `delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/base.rb:442:in `delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/persistence.rb:72:in `delete'
from (irb):60
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
如果我这样做
person.destroy
我得到了
SQL (0.2ms) BEGIN
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
Mysql::Error: Unknown column 'persons.' in 'where clause': DELETE FROM `persons` WHERE `persons`.`` = ?
(0.1ms) ROLLBACK
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'persons.' in 'where clause': DELETE FROM `persons` WHERE `persons`.`` = ?
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:883:in `prepare'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:883:in `exec_stmt'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:490:in `block in exec_delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:489:in `exec_delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/relation.rb:351:in `delete_all'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/persistence.rb:94:in `destroy'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/locking/optimistic.rb:119:in `destroy'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/callbacks.rb:254:in `block in destroy'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/callbacks.rb:254:in `destroy'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/transactions.rb:236:in `block in destroy'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/transactions.rb:208:in `transaction'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from .rvm/gems/ruby-1.9.2-p320@rails-3.1.1/gems/activerecord-3.1.1/lib/active_record/transactions.rb:236:in `destroy'
from (irb):61
但是如果我写一个SQL删除语句,我可以通过MySQL控制台删除该记录。知道可能会发生什么吗?
其他信息: Person表有一个id列,但数据库中的主键是使用id和其他一些列的复合键。人员模型是由其他人编写的,并且编写了许多方法,但没有明确写出任何名为delete或destroy的方法。
答案 0 :(得分:2)
@person = Person.first
@person.destroy
如果您使用非标准主键,则需要先设置它:
Person.primary_key = 'person_id'
@person = Person.first
@person.destroy
如果其他所有方法都失败了......我输入的信息很糟糕:
Person.connection.execute("delete from persons where id = 1 and state_id = 2 and project_id = 5")
为了节省您当前和未来的噩梦,您应该重新构建数据库,以遵循以标准方式存储数据。人员表应仅包含具有唯一ID列的唯一人员,该列是主键。应创建新的project_assignments表/模型来处理具有自己唯一ID的赋值,以及state_id,project_id和person_id。属于Person的数据应该驻留在该表中,分配数据应该移动到project_assignments等。