# Adding records to a nested model through an association

**URL:** <https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233>\
**Category:** rubyonrails-talk\
**Created:** [August 14, 2018, 9:49pm UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233 "2018-08-14T21:49:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Sanderbeck](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@John\_Sanderbeck](https://discuss.rubyonrails.org/u/John_Sanderbeck)\
**Post date:** [August 14, 2018, 9:49pm UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/1 "2018-08-14T21:49:02Z")

</div>

Not sure if the subject is correct for what I am trying to do…

I am working on a School application and I added an Attendance section

There is an attendance record that is associated to a student record

The student record has an associated model called notes…

What I would like to be able to do is add notes to the student through the attendance record…

My Attendance record belongs\_to :student

My Student record has\_many :attendances

My Student record has\_many :notes

I can access Attendance.student.notes just fine, but when I try to add notes through a nested form it doesn’t like that…

Also just tried adding has\_many :notes, through: :student and now I can access Attendance.notes, however when I add

\<%= f.fields\_for :notes %\>

\<%= f.link\_to\_add “Add Note”, :notes, class: ‘btn btn-xs btn-primary btn-padded’ %\>

I get an error no block given(yield)

Any suggestions on how to code the nested form? Is this a routes issue?

John

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [August 15, 2018, 12:03am UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/2 "2018-08-15T00:03:19Z")

</div>

> Not sure if the subject is correct for what I am trying to do...
> 
> I am working on a School application and I added an Attendance section
> 
> There is an attendance record that is associated to a student record
> 
> The student record has an associated model called notes...
> 
> What I would like to be able to do is add notes to the student through the attendance record...
> 
> My Attendance record belongs\_to :student My Student record has\_many :attendances My Student record has\_many :notes
> 
> I can access Attendance.student.notes just fine, but when I try to add notes through a nested form it doesn't like that...
> 
> Also just tried adding has\_many :notes, through: :student and now I can access Attendance.notes, however when I add
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.fields\_for :notes %\>

You need to add a 'do' at the end of the previous line. Without that, and...

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.link\_to\_add "Add Note", :notes, class: 'btn btn-xs btn-primary btn-padded' %\>

an end here to close the block, you're not giving a black to the fields\_for macro. That's what the error means.

Walter

---

<div class="post-metadata">

**Author:** ![John\_Sanderbeck](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@John\_Sanderbeck](https://discuss.rubyonrails.org/u/John_Sanderbeck)\
**Post date:** [August 15, 2018, 10:26am UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/3 "2018-08-15T10:26:31Z")

</div>

That’s the way all my other nests are defined and they work fine… I think the problem is I am adding a nest from a different model than I am actually working on…

---

<div class="post-metadata">

**Author:** ![John\_Sanderbeck](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@John\_Sanderbeck](https://discuss.rubyonrails.org/u/John_Sanderbeck)\
**Post date:** [August 15, 2018, 10:42am UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/4 "2018-08-15T10:42:38Z")

</div>

The way the nests are working now is it should render the file \_note\_fields.html.erb but it acts like it is not finding that file which tells me the fields\_for is looking for a different name due to the way it is nested

---

<div class="post-metadata">

**Author:** ![John\_Sanderbeck](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@John\_Sanderbeck](https://discuss.rubyonrails.org/u/John_Sanderbeck)\
**Post date:** [August 16, 2018, 8:59pm UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/5 "2018-08-16T20:59:59Z")

</div>

I figured it out… Didn’t have a accepts\_nested\_attributes\_for :notes in my attendance model… Duh !!!

However when I try to write I get an error

Cannot modify association ‘Attendance#notes’ because the source reflection class ‘Note’ is associated to ‘Student’ via :has\_many.

---

<div class="post-metadata">

**Author:** ![John\_Sanderbeck](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@John\_Sanderbeck](https://discuss.rubyonrails.org/u/John_Sanderbeck)\
**Post date:** [August 17, 2018, 1:47pm UTC](https://discuss.rubyonrails.org/t/adding-records-to-a-nested-model-through-an-association/73233/6 "2018-08-17T13:47:05Z")

</div>

Is this the proper relationship for this to work? It looks right to me…

What I want to do is add notes to a student through the Attendance Edit form

class Attendance \< ActiveRecord::Base

belongs\_to :student

accepts\_nested\_attributes\_for :student

has\_many :notes, through: :student

accepts\_nested\_attributes\_for :notes

end

class Student \< ActiveRecord::Base

# Also used through Attendance Edit

has\_many :attendances, dependent: :destroy

has\_many :notes, dependent: :destroy

accepts\_nested\_attributes\_for :notes, allow\_destroy: true

end

class Note \< ActiveRecord::Base

belongs\_to :student

end
