当我进行1:1关联时,我在ROR中遇到问题
我的用户模型包含用户和密码字段,我的配置文件模型包含name,age和user_id字段。
class Profile < ActiveRecord::Base
belongs_to :User
end
class User < ActiveRecord::Base
has_one :profile
end
通过ROR控制台获取信息
user = User.new
user.user = "XXXX"
user.pass = "12345"
user.save
profile = profile.new
profile.name = "XXXX"
profile.age = 20
profile.save
到目前为止一切都很好......但是,当我建立联想时
user.profile = profile
给我错误:
NameError: undefined local variable or method `profile' for main:Object
from (irb):5
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/commands/rails.rb:6:in `call'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/command_wrapper.rb:38:in `call'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:185:in `block in serve'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:156:in `fork'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:156:in `serve'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:131:in `block in run'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:125:in `loop'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application.rb:125:in `run'
from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.6.2/lib/spring/application/boot.rb:18:in `<top (required)>'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from
schema.db是:
ActiveRecord::Schema.define(version: 20160219230003) do
create_table "profiles", force: :cascade do |t|
t.string "name"
t.integer "age"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "user"
t.string "pass"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
解决这个问题的方法是什么?
我有ruby 2.3.0p0和Rails 4.2.5
感谢!
答案 0 :(得分:2)
profile = profile.new # <-- Error here
profile.name = "XXXX"
profile.age = 20
profile.save
例外说:
NameError:未定义的局部变量或方法`profile' 主:对象
main:Object
是您的控制台,配置文件未定义,应该是:
profile = Profile.new
答案 1 :(得分:0)
user = User.new
user.user = "..."
user.pass = "1234"
user.profile = Profile.new
user.profile.name = "..."
user.profile.age = 20
user.save