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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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].saveOn Sep 7, 12:37 pm, KevinJones <kev.r.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---
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 On Sun, Sep 7, 2008 at 5:46 AM, Dejan Dimic <dejan.dimic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > > On Sep 7, 12:37 pm, KevinJones <kev.r.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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 > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks all, so I was right. I was missing something fundamental :) Kevin On Sep 7, 2:04 pm, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On Sun, Sep 7, 2008 at 5:46 AM, Dejan Dimic <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > On Sep 7, 12:37 pm, KevinJones <kev.r.jo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---