gpt4 book ai didi

ruby-on-rails - NoMethodError : undefined method `each' for #

转载 作者:太空宇宙 更新时间:2023-11-03 17:23:42 25 4
gpt4 key购买 nike

尝试在个人资料和教育之间创建一对多关系。我在尝试向个人资料添加教育时遇到此错误。

.-(~/scratch/rails_projects/rezumei)-----------------------------------------------------------------------------------------------------------(patrick@tesla)-
`--> rails c
Loading development environment (Rails 4.0.2)
irb(main):001:0> patrick = User.new
=> #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, profile_id: nil>
irb(main):002:0> patrick.profile = Profile.new
=> #<Profile id: nil, first_name: nil, last_name: nil, interests: nil, headline: nil, created_at: nil, updated_at: nil, user_id: nil, profile_id: nil, maiden_name: nil, industry: nil, summary: nil, specialties: nil, picture_url: nil, email_address: nil, skills: nil, phone_numbers: nil, main_address: nil>
irb(main):003:0> patrick.profile.educations = Education.new
NoMethodError: undefined method `each' for #<Education id: nil, profile_id: nil, school_name: nil>
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:152:in `method_missing'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/collection_association.rb:333:in `replace'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/collection_association.rb:42:in `writer'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:78:in `educations='
from (irb):3
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/patrick/scratch/rails_projects/rezumei/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):004:0>

class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end

class Profile < ActiveRecord::Base
belongs_to :user
has_many :educations, dependent: :destroy
end

class Education < ActiveRecord::Base
belongs_to :profile
end

最佳答案

这可用于将 Education 添加到 educations 中:

patrick.profile.educations << Education.new

关于ruby-on-rails - NoMethodError : undefined method `each' for #<Education id: nil, profile_id : nil, school_name:无>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com