问题
I have a table, say Persons and if I do
person = Person.find(:first)
person.delete
I get
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>'
If I do
person.destroy
I get
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
But I can delete the record through MySQL console if I write a SQL delete statement. Any idea what might be happening?
Other information: The Person table has an id column, but the primary key in the databae is a composite key using the id and some other columns. The person model was written by someone else, and has a number of methods written, but none named delete or destroy were written explicitly.
回答1:
@person = Person.first
@person.destroy
If you used a non standard primary key, you will need to set it first:
Person.primary_key = 'person_id'
@person = Person.first
@person.destroy
If all else fails ... I feel bad for typing this:
Person.connection.execute("delete from persons where id = 1 and state_id = 2 and project_id = 5")
To save yourself current and future nightmares, you should restructure your database to follow rails standard way of storing data. Person table should contain only unique people with unique id column that is a primary key. New project_assignments table/model should be created to handle assignments with it's own unique id, as well as state_id, project_id, and person_id. Data that belongs with Person should reside in that table, assignment data should be moved to project_assignments etc.
来源:https://stackoverflow.com/questions/12751954/cant-delete-record-through-rails