Hi, I''m having some problems using ActiveRecord to update records in a sqlite database. This isn''t exactly as rails question, since I''m doing this initial import outside of rails, but I''m hoping someone hear might have an idea as to what my problem is. The basic problem is with the following code (which is run in a big loop) m = Movie.find_by_title_and_year title, year if m #puts "#{title} $#{budget}" puts m.update_attribute("budget", budget) end it prints true, indicating that the record has been saved succesfully - but when I look in the database (select * from movies where budget <> "";) no records have been updated. Does anyone have any ideas? I''d also like to know how to hook up a logger outside of the rails environment - can anyone tell me how I can set it up? Thanks for your help, Hadley
On 13:21 Sat 12 Feb , hadley wickham wrote:> Hi, > > I''m having some problems using ActiveRecord to update records in a > sqlite database. This isn''t exactly as rails question, since I''m > doing this initial import outside of rails, but I''m hoping someone > hear might have an idea as to what my problem is. > > The basic problem is with the following code (which is run in a big loop) > > m = Movie.find_by_title_and_year title, year > if m > #puts "#{title} $#{budget}" > puts m.update_attribute("budget", budget) > endThe update_attribute method does not actually save the record--it just updates the in-memory copy. You need to do "m.save" to actually save the record.> I''d also like to know how to hook up a logger outside of the rails > environment - can anyone tell me how I can set it up?I think it''s just a matter of doing: ActiveRecord::Base.logger = Logger.new("logfile.log") And then using logger.info "blah" logger.debug "foo" logger.warn "bar" In your AR objects. - Jamis -- Jamis Buck jamis_buck-8Bzd4dk9+oo@public.gmane.org http://jamis.jamisbuck.org ------------------------------ "I am Victor of Borge. You will be assimil-nine-ed."
> The update_attribute method does not actually save the record--it just > updates the in-memory copy. You need to do "m.save" to actually save > the record.That''s not what the documentation says - see http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000209, but I tried adding a .save anyway and that didn''t help.> ActiveRecord::Base.logger = Logger.new("logfile.log") > > And then using > > logger.info "blah" > logger.debug "foo" > logger.warn "bar"Sorry - I wasn''t very clear here. I meant the logger that automatically logs all the sql queries. Thanks, Hadley
> Sorry - I wasn''t very clear here. I meant the logger that > automatically logs all the sql queries.Solved my own problem. Once I got logging going (ActiveRecord::Base.logger = Logger.new(STDOUT)) I discovered pretty quickly that I forgetten to add an auto incremenet to my primary key. Ooops! Hadley
Apparently Analagous Threads
- Don''t Try This at Home (rails --help)
- saving an ActiveRecord without trigging the callbacks
- acts_as_queue
- Why does ActiveRecord allow perception of success when updating an ID, however it doesn't really work(i.e. no change in database)?
- ActiveRecord increment confusion