What's the new before_create in Rails 4? -
i tried using before_create:
class star < activerecord::base before_create :add_to_total_stars
post load (9.7ms) select "posts".* "posts" "posts"."id" = ? order "posts"."id" asc limit 1 [["id", 1]] (1.0ms) rollback transaction nomethoderror: undefined method `+' nil:nilclass
but can see i'm getting error.
what's new before_create in rails 4?
edit:
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 `+' nil:nilclass /home/alex/rails/rating/app/models/star.rb:10:in `add_to_total_stars' /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' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `create_record' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/timestamp.rb:57:in `create_record' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:466:in `create_or_update' /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' /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' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `create_or_update' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:106:in `save' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/validations.rb:51:in `save' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:32:in `save' /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' /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' /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' /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' /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' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction' /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' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in `block in save' /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!' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:269:in `save' (irb):5 /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' /home/alex/.rvm/gems/ruby-1.9.3-p0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>'1.9.3-p0 :006 > ^c 1.9.3-p0 :006 > alex@alex-k43u:~/rails/rating$ rails c
Comments
Post a Comment