Skip to content

Instantly share code, notes, and snippets.

@superlou
Created March 19, 2012 01:04
Show Gist options
  • Save superlou/2088470 to your computer and use it in GitHub Desktop.
Save superlou/2088470 to your computer and use it in GitHub Desktop.
Undefined 'name' for nil:NilClass
class Con < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
after_create :create_tag
after_save :rename_tag
def create_tag
tag = Tag.new(:name => self.name, :tag_group => TagGroup.find_by_name('Cons'))
tag.save
end
def rename_tag
tag = Tag.find_by_group_and_tag_name('Cons', self.name)
puts tag
name = tag.name
puts name
end
end
class ConsController < ApplicationController
def create
con = Con.new(params[:con])
if con.save!
redirect_to con, :notice => "Con created successfully"
end
end
end
require 'test_helper'
class ConsControllerTest < ActionController::TestCase
context "with a TagGroup called 'Cons'" do
setup do
Factory :tag_group, :name => "Cons"
end
should "create a tag when created" do
assert_difference('Tag.count') do
post :create, :con => {:name => "New Con"}
end
tag = Tag.find_by_name("New Con")
assert_not_nil tag
assert_equal TagGroup.find_by_name('Cons'), tag.tag_group
end
#### THE OFFENDING TASK ####
should "rename its tag when renamed" do
con = Factory(:con, :name=>"New Con")
tag = Tag.find_by_name(con.name)
assert_equal "New Con", tag.name
con.name = "Changed Name"
con.save
#assert_equal "Changed Name", tag.name
end
should "not remove its tag when deleted" do
assert false
end
end
end
#<Tag:0x00000005002fd0>
New Con
NoMethodError: undefined method `name' for nil:NilClass
/home/lsimons/RubymineProjects/connie/app/models/con.rb:16:in `rename_tag'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `_run__1766852513517797900__save__1160347338664056617__callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/callbacks.rb:264:in `create_or_update'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/persistence.rb:84:in `save'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/validations.rb:50:in `save'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/attribute_methods/dirty.rb:22:in `save'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:208:in `transaction'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:241:in `block in save'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.2/lib/active_record/transactions.rb:240:in `save'
/home/lsimons/RubymineProjects/connie/test/functional/cons_controller_test.rb:27:in `block (2 levels) in <class:ConsControllerTest>'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-context-1.0.0/lib/shoulda/context/context.rb:398:in `call'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/shoulda-context-1.0.0/lib/shoulda/context/context.rb:398:in `block in create_test_from_should_hash'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:1046:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:458:in `_run__574566149628552421__setup__1160347338664056617__callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.2/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/test_runner.rb:21:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:68:in `_run_test'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `block in _run_suite'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:55:in `_run_suite'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `block (2 levels) in _run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:40:in `block in _run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:106:in `fix_sync'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-reporters-0.5.1/lib/minitest/suite_runner.rb:39:in `_run_anything'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:952:in `run_tests'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:939:in `block in _run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:938:in `each'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:938:in `_run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:927:in `run'
/home/lsimons/.rvm/gems/ruby-1.9.2-p290/gems/minitest-2.11.3/lib/minitest/unit.rb:695:in `block in autorun'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment