Modelのカラム名でアクセスするとNoMethodError
Modelのカラム名にうっかりclassとか付けちゃうとカラム名でアクセスしたとき例外が出て詰みます
testmodel
class CreateTestmodels < ActiveRecord::Migration def self.up create_table :testmodels do |t| t.string :name t.integer :class t.timestamps end end def self.down drop_table :testmodels end end
実行
$rails c Loading development environment (Rails 3.0.5) ruby-1.9.2-p180 :001 > m = Testmodel.new => #<Testmodel id: nil, name: nil, class: nil, created_at: nil, updated_at: nil> ruby-1.9.2-p180 :002 > m.name = "aaa"
例外
NoMethodError: undefined method `private_method_defined?' for nil:NilClass from /Users/user/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing' from /Users/user/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:403:in `guard_private_attribute_method!' from /Users/user/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:43:in `method_missing'