试图在个人资料和教育之间建立一对多的关系我在尝试将教育添加到配置文件时遇到此错误。
.-(~/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:#<教育ID:无,profile_id:无,school_name:无>的未定义方法`each',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21333447/