我正在使用rails 4.2.6和ruby 2.2.3
我有两个型号: 用户和帐户
user.rb:
class User < ActiveRecord::Base
has_many :accounts
end
account.rb:
class Account < ActiveRecord::Base
belongs_to :user
end
迁移:
create_users:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps null: false
end
end
end
create_accounts:
class CreateAccounts < ActiveRecord::Migration
def change
create_table :accounts do |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) do
create_table "accounts", force: :cascade do |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 do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
在rails控制台中,我有:
User.first:
User.first
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "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".* FROM "accounts" ORDER BY "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".* FROM "accounts" ORDER BY "accounts"."id" ASC LIMIT 1
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "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">
但是当我尝试获取用户帐户时我得到了错误: User.first.accounts:
User.first.accounts
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
NoMethodError: undefined method `foreign_key' for 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>