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_experience
when using a has_one
or a belongs_to
association. It's working for webconnection
because it is a has_one
association.
There's a difference with has_many
associations though: you must call the build
method on the association, like this: resume.experiences.build
. This indicates
Because this is a has_many
association and not a has_one
or a belongs_to
, you should be using resume.experiences.build
.