On Dec 29, 2010, at 11:33 AM, Jim Burgess wrote:
> Hi,
>
> I'm trying to understand some code in a book I'm reading and was hoping
> someone could tell me if I have understood it correctly.
>
> There are two models, an employee model and a department model. A
> department has_many :employees and an employee belongs_to :department.
>
> Here's a snippet of code from the form to edit an existing employee:
>
> <% fields_for( @employee.department) do |department_f| %>
> <p>
> <%= department_f.label :name,"Department name" %><br />
> <%= department_f.text_field :name %>
> </p>
> <% end %>
>
> 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?
Yes. And it only does this if the update to @employee was successful first.
-philip
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment