Given the current setup, the simplest thing would probably be to raise an exception using #save!
:
def apply_to(listing)
jobapp = Jobapp.new(resume: self, listing: listing)
jobapp.save! # note the bang (!)
end
And in the controller
def apply
set_resume
@resume.apply_to(@listing)
redirect_to @listing
rescue ActiveRecordError
redirect_to @listing, alert: 'Unable to apply for job'
end
It may be worth the effort to rename your method to apply_to!
(also with a bang) to make it clear that it can raise an exception.
As for why create
doesn't work, it's not that it returns true
- it returns the new record (and all objects except nil and false are considered "true"). To check if create
actually managed to persist that record in the database, you can check record.persisted?
. If true, then it was saved just fine.
So using your create
version of the method, you could do
def apply
set_resume
jobapp = @resume.apply_to(@listing)
if jobapp.persisted?
redirect_to @listing
else
redirect_to @listing, alert: 'Unable to apply for job'
end
end
This would also let you access jobapp.errors
, if you want.
A few other things caught my eye:
set_resume
should probably be in a before-filter. Or be a method that just returns a record, leaving it up to the caller to stick it in an instance variable (or not)
I imagine a jobapp
belongs to a listing
(and/or a resume
). If so, it'd be cleaner to build it like so:
jobapp = owner.jobapps.new(other: foo)
Given that it's this simple, though, it might be something you could just do in the controller.
It's never quite clear where a join model like jobapp
(which I assume is basically a join model) should be created. Should it be in the Resume
model or in the Listing
model? Either one makes sense. So the Solomonic solution is do neither (at least for now), and handle it in the controller:
def apply
jobapp = @listing.jobapps.new(resume: @resume)
if jobapp.save
redirect_to @listing
else
redirect_to @listing, alert: 'Unable to apply for job'
end
end
Lastly, you might want to tweak that error message. It seems a little "blunt" to me. Though it's the server that failed, it sounds as if the user is unable to apply for the job. "Failed to save job application" might be more appropriate