0

我有一张桌子,说人,如果我这样做

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 和其他一些列的复合键。person 模型是由其他人编写的,并且编写了许多方法,但没有一个名为 delete 或 destroy 的方法被显式编写。

4

1 回答 1

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")

为了避免当前和未来的噩梦,您应该重构您的数据库以遵循 Rails 存储数据的标准方式。人员表应仅包含具有唯一 id 列的唯一人员,该列是主键。应该创建新的 project_assignments 表/模型来处理具有自己唯一 ID 以及 state_id、project_id 和 person_id 的分配。属于 Person 的数据应驻留在该表中,分配数据应移动到 project_assignments 等。

于 2012-10-05T18:29:26.173 回答