ruby on rails - NoMethodError: undefined method `foreign_key' for ActiveSupport::Inflector:Module -


i'm using rails 4.2.6 , ruby 2.2.3

i have 2 models: user , account

user.rb:

class user < activerecord::base    has_many :accounts  end

account.rb:

class account < activerecord::base    belongs_to :user  end

migrations:

create_users:

class createusers < activerecord::migration    def change      create_table :users |t|        t.string :name          t.timestamps null: false      end    end  end

create_accounts:

class createaccounts < activerecord::migration    def change      create_table :accounts |t|        t.integer :number        t.references :user, index: true, foreign_key: true          t.timestamps null: false      end    end  end

schema.rb:

activerecord::schema.define(version: 20160808203550)      create_table "accounts", force: :cascade |t|      t.integer  "number"      t.integer  "user_id"      t.datetime "created_at", null: false      t.datetime "updated_at", null: false    end      add_index "accounts", ["user_id"], name: "index_accounts_on_user_id"      create_table "users", force: :cascade |t|      t.string   "name"      t.datetime "created_at", null: false      t.datetime "updated_at", null: false    end    end

in rails console have:

user.first:

user.first    user load (0.2ms)  select  "users".* "users"  order "users"."id" asc limit 1  => #<user id: 1, name: "alisson", created_at: "2016-08-08 20:36:09", updated_at: "2016-08-08 20:36:09">  irb(main):002:0> 

account.first:

account.first    account load (0.2ms)  select  "accounts".* "accounts"  order "accounts"."id" asc limit 1  => #<account id: 1, number: 124212, user_id: 1, created_at: "2016-08-08 20:36:27", updated_at: "2016-08-08 20:36:27">  irb(main):003:0> 

account.first.user:

account.first.user    account load (0.3ms)  select  "accounts".* "accounts"  order "accounts"."id" asc limit 1    user load (0.4ms)  select  "users".* "users" "users"."id" = ? limit 1  [["id", 1]]  => #<user id: 1, name: "alisson", created_at: "2016-08-08 20:36:09", updated_at: "2016-08-08 20:36:09">

but when try user's accounts got , error: user.first.accounts:

user.first.accounts    user load (0.3ms)  select  "users".* "users"  order "users"."id" asc limit 1  nomethoderror: undefined method `foreign_key' activesupport::inflector:module  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/core_ext/string/inflections.rb:215:in `foreign_key'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/reflection.rb:572:in `derive_foreign_key'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/reflection.rb:330:in `foreign_key'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/reflection.rb:163:in `join_keys'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:99:in `last_chain_scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:139:in `add_constraints'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:39:in `scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association_scope.rb:5:in `scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association.rb:97:in `association_scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/association.rb:86:in `scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:423:in `scope'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/collection_proxy.rb:37:in `initialize'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:106:in `new'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:106:in `create'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:41:in `reader'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.6/lib/active_record/associations/builder/association.rb:115:in `accounts'  ... 11 levels...  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'  	from /home/alisson/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'  	from -e:1:in `<main>'irb(main):005:0> 


Comments