Reputation: 47
I have the problem in ROR when I make the 1:1 association
I have the User model with user and password fields and I have the Profile model with name, age and user_id fields.
class Profile < ActiveRecord::Base
belongs_to :User
end
class User < ActiveRecord::Base
has_one :profile
end
Income the information is through the ROR console
user = User.new
user.user = "XXXX"
user.pass = "12345"
user.save
profile = profile.new
profile.name = "XXXX"
profile.age = 20
profile.save
all good so far... but, when I make the association
user.profile = profile
gives me the error:
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
And the schema.db is:
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
Any solution to this problem?
I have ruby 2.3.0p0 and Rails 4.2.5
Thank!
Upvotes: 0
Views: 102
Reputation: 11193
user = User.new
user.user = "..."
user.pass = "1234"
user.profile = Profile.new
user.profile.name = "..."
user.profile.age = 20
user.save
Upvotes: 0
Reputation: 6707
profile = profile.new # <-- Error here
profile.name = "XXXX"
profile.age = 20
profile.save
The exception said:
NameError: undefined local variable or method `profile' for main:Object
main:Object
is your console, and profile is undefined, it should be:
profile = Profile.new
Upvotes: 2