Controllers
class ExperiencesController < ApplicationController
def create
@resume = Resume.find(params[:resume])
@experience = @resume.build_experience(params[:experience])
end
end
class ResumesController < ApplicationController
def create
@resume = Resume.new(params[:resume])
@resume.build_webconnection
@resume.build_experience # <<<<<<<<< Error occurs here
if @resume.save
#UserMailer.created_resume_email(@user).deliver
redirect_to @resume
else
@title = "Create a new resume"
render :action => "new"
end
end
end
Models
class Experience < ActiveRecord::Base
belongs_to :resume
end
class Resume < ActiveRecord::Base
has_one :webconnection
has_many :experiences
end
Error Message when I try to create a Resume (which also creates an Experience associated with Resume)
NoMethodError in ResumesController#create
undefined method `build_experience' for #<Resume:0xbb428a4>
I feel like I have everything pretty much in place, but missing an ‘s’ or something somewhere. Any idea why I’m getting this error?
You would normally use
build_experiencewhen using ahas_oneor abelongs_toassociation. It’s working forwebconnectionbecause it is ahas_oneassociation.There’s a difference with
has_manyassociations though: you must call thebuildmethod on the association, like this:resume.experiences.build. This indicatesBecause this is a
has_manyassociation and not ahas_oneor abelongs_to, you should be usingresume.experiences.build.