The form is submitted to the update action of ‘employees_controller.rb’,
where there is the following code:
if @employee.update_attributes(params[:employee]) && @employee.department.update_attributes(params[:department])
… employee successfully updated …
end
My question is about the line
“@employee.department.update_attributes(params[:department])”
Is it correct to say that this code updates the attributes (in this case
“name”) of the Department object that is linked (via the “has_many” and
“belongs_to” relationship) to the Employee object currently being
edited?
<% fields_for( @employee.department) do |department_f| %> @employee.department.update_attributes(params[:department])
… employee successfully updated …
end
My question is about the line
“@employee.department.update_attributes(params[:department])”
Is it correct to say that this code updates the attributes (in this case
“name”) of the Department object that is linked (via the “has_many” and
“belongs_to” relationship) to the Employee object currently being
edited?
Yes. And it only does this if the update to @employee was successful
first.
Thanks for the answer Philip. It is good to get confirmation
that I have understood things correctly.
Yes. And it only does this if the update to @employee was successful
first.
Is the order particularly significant in this case (i.e. that it
attempts to update @employee first)?
No technical reason (as this is an update, not a create), but logically
it would make sense that you’d want to update them in that order.
Obviously it would be bad to update one record and not the other, but
won’t rails throw an error and re-render the ‘edit’ view if validation
for either @employee or @employee.department fails?
Nope. update_attributes simply returns true/false depending on whether
or not it succeeded. There are ways to make it raise an error, but your
code isn’t doing that.
Thanks for the answer Philip. It is good to get confirmation
that I have understood things correctly.
Yes. And it only does this if the update to @employee was successful
first.
Is the order particularly significant in this case (i.e. that it
attempts to update @employee first)?
Obviously it would be bad to update one record and not the other, but
won’t rails throw an error and re-render the ‘edit’ view if validation
for either @employee or @employee.department fails?
No technical reason (as this is an update, not a create), but logically
it would make sense that you’d want to update them in that order.
Cool. That was exactly the conclusion I’d drawn.
won’t rails throw an error and re-render the ‘edit’ view if validation
for either @employee or @employee.department fails?
Nope. update_attributes simply returns true/false depending on whether
or not it succeeded. There are ways to make it raise an error, but your
code isn’t doing that.
Yeah, sorry, I guess I didn’t include enough code with my original
question.
I’m (the book is) using the scaffold generator to create both ‘employee’
and ‘department’ resources. The complete update method attempts to
update the attributes of @employee and @employee.department. If this
doesn’t work (as validation has failed, for example) it re-renders the
action ‘edit’.
def update @employee = Employee.find(params[:id])
respond_to do |format|
if @employee.update_attributes(params[:employee]) && @employee.department.update_attributes(params[:department])
format.html { redirect_to(@employee, :notice => ‘Employee was
successfully updated.’) }
format.xml { head :ok }
else @departments = Department.find(:all)
format.html { render :action => “edit” }
format.xml { render :xml => @employee.errors, :status =>
:unprocessable_entity }
end
end
end
I only bring it up because Rails/Ruby can throw errors and raise
exceptions (which are themselves different) so it can get confusing if
you play loose with the terminology
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.