Accepts_nested_attributes_for abilities

Is there a way to use accepts_nested_attributes_for to update another
table from within a controller. For example, I have this in my students
controller:

def student_fail
@student = Subject.find(params[:id]) if params[:id]
@student.build_student_fail
end

def student_fail_finalize
if @student.update_attributes params[:student]
@subject.save
end
end

I have this model relationship:

Student
has_one :student_fail
accepts_nested_attributes_for :student_fail, :allow_destroy => true

StudentFail
belongs_to :student_fail_state
belongs_to :student

StudentFailState
has_many :students, :through => :student_fail
has_many :student_fails

From the students controller, when the user fills out a form, I just
want to update that information to the student_fail table. Can I achieve
this through the controller using accepts_nested_attributes_for? I don’t
want to add anything to the view that indicates the other model. I’m
hoping to do it directly in the controller. Thanks for any suggestions.

John M. wrote:

Is there a way to use accepts_nested_attributes_for to update another
table from within a controller. For example, I have this in my students
controller:

def student_fail
@student = Subject.find(params[:id]) if params[:id]
@student.build_student_fail
end

def student_fail_finalize
if @student.update_attributes params[:student]
@subject.save
end
end

I have this model relationship:

Student
has_one :student_fail
accepts_nested_attributes_for :student_fail, :allow_destroy => true

StudentFail
belongs_to :student_fail_state
belongs_to :student

StudentFailState
has_many :students, :through => :student_fail
has_many :student_fails

From the students controller, when the user fills out a form, I just
want to update that information to the student_fail table. Can I achieve
this through the controller using accepts_nested_attributes_for? I don’t
want to add anything to the view that indicates the other model. I’m
hoping to do it directly in the controller.

Then you don’t need accepts_nested_attributes_for.

However, the controller is the wrong place to sort out the attributes.
Do it in a model or introduce a presenter class. Generally speaking,
any but the most rudimentary logic does not go in the controller.

Thanks for any suggestions.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]