我尝试使用 before_create:
类星 < ActiveRecord::Base before_create :add_to_total_stars
Post Load (9.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY "posts"."id" ASC LIMIT 1 [["id", 1]] (1.0ms) 回滚事务 NoMethodError: undefined method `+' for nil:NilClass
但正如你所看到的,我遇到了一个错误。
Rails 4 中的新 before_create 是什么?
编辑:
class Star < ActiveRecord::Base before_create :add_to_total_stars
belongs_to :starable, :polymorphic => true
protected
def add_to_total_stars
if [Post].include?(starable.class)
self.starable.update_column(:total_stars, starable.total_stars + self.number)
end end end
NoMethodError: undefined method `+' for nil:NilClass from /home/alex/rails/rating/app/models/star.rb:10:in `add_to_total_stars' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:377:in `_run__956917800__create__callbacks' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `create_record' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/timestamp.rb:57:in `create_record' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:466:in `create_or_update' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `block in create_or_update' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:383:in `_run__956917800__save__callbacks' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `create_or_update' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:106:in `save' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/validations.rb:51:in `save' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:32:in `save' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in `block (2 levels) in save' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in `block in save' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:281:in `rollback_active_record_state!' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:269:in `save' from (irb):5 from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' from /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' from bin/rails:4:in `require' from bin/rails:4:in `<main>'1.9.3-p0 :006 > ^C
1.9.3-p0 :006 > alex@alex-K43U:~/rails/rating$ rails c