# ActiveRecord not writing to tables

**URL:** <https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947>\
**Category:** Rails\
**Created:** [September 7, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947 "2008-09-07T12:41:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KevinJones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@KevinJones](https://www.ruby-forum.com/u/KevinJones)\
**Post date:** [September 7, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947/1 "2008-09-07T12:41:44Z")

</div>

I have two tables in MySQL: football\_fixtures and weekly\_fixtures.  
In my code I have two classes  
class FootballFixture  
end

and

class WeeklyFixture  
has\_many :football\_fixtures  
end

the WeeklyFixture class has more code than this, mostly validation

In the console window I do this

wf = WeeklyFixture.new  
wf.somefield = somevalue  
wf.save!

and everything is OK

however if I do

wf = WeeklyFixture(6) # where 6 is an id in the table so the wf is  
found!  
wf.football\_fixtures[0].name = “value”  
wf.save!

I get a return value of true, but no data is written to the database.

I have the log visible on another terminal (tail -f development.log),  
and I don’t see any update call being made. (I do see selects being  
made when I read the records)

I think I’m missing something fundamental here. I assumed that when I  
updated the related fields and wrote the parent model the the DB then  
the save would also write the related fields, but this is not  
happening.

BTW, if I do  
wf.football\_fixtures[0].save

that does work!

What (presumably simple) magic incantation am I missing?

Thanks,

Kevin Jones

---

<div class="post-metadata">

**Author:** ![KevinJones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@KevinJones](https://www.ruby-forum.com/u/KevinJones)\
**Post date:** [September 7, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947/2 "2008-09-07T14:47:36Z")

</div>

When you do this code

> wf = WeeklyFixture(6) # where 6 is an id in the table so the wf is  
> found!  
> wf.football\_fixtures[0].name = “value”  
> wf.save!

the wf has not been changed in any way.  
It point in the same football\_fixtures item by its foreign key.

The football\_fixtures has changed and you should save that change.  
That’s done by your second sample.

> wf.football\_fixtures[0].save

---

<div class="post-metadata">

**Author:** ![KevinJones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@KevinJones](https://www.ruby-forum.com/u/KevinJones)\
**Post date:** [September 7, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947/3 "2008-09-07T15:05:45Z")

</div>

Hi, there’s no magic happening. You’re simply invoking save on the  
wrong  
instance. Just because one invokes save on a parent instance it doesn’t  
automatically invoke save on its children. In most cases, this would be  
very inefficient. I would recommend learning/understanding more about  
ActiveRecord because this will help in the long run.  
Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![KevinJones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@KevinJones](https://www.ruby-forum.com/u/KevinJones)\
**Post date:** [September 7, 2008, 8:13pm UTC](https://www.ruby-forum.com/t/activerecord-not-writing-to-tables/147947/4 "2008-09-07T20:13:35Z")

</div>

Thanks all,

so I was right. I was missing something fundamental 🙂

Kevin
