ActiveRecord not writing to tables

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

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.

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

Thanks all,

so I was right. I was missing something fundamental :slight_smile:

Kevin