hi, another newbie question ;) I have a model Medium (from which I get subclasses Photo, Video and Audio), which I save to the database, and then use the generated id to name the equivalent uploaded file, which I save to the filesystem: class Medium < ActiveRecord::Base belongs_to :user attr_accessor :file attr_accessor :format attr_accessor :path def format=(f) format = f end def path=(p) path = p end alias :old_save :save def save old_save path = "/media/" + user.id.to_s + "/" + id.to_s + "." + format File.open( (File.expand_path(RAILS_ROOT) + "/public" + path), "wb") { |f| f.write(file.read) } end end when trying to upload it, I get a "cannot convert nil into String", referring to Medium.id (I tested... user.id.to_s comes out fine) once I do ''old_save'', shouldn''t Medium.id be automatically by ActiveRecord? thanks in advance, Oliver
-----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of oliver barnes Sent: Tuesday, August 02, 2005 7:26 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] can''t get id after saving model hi, another newbie question ;) I have a model Medium (from which I get subclasses Photo, Video and Audio), which I save to the database, and then use the generated id to name the equivalent uploaded file, which I save to the filesystem: class Medium < ActiveRecord::Base belongs_to :user attr_accessor :file attr_accessor :format attr_accessor :path def format=(f) format = f end def path=(p) path = p end alias :old_save :save def save old_save path = "/media/" + user.id.to_s + "/" + id.to_s + "." + format File.open( (File.expand_path(RAILS_ROOT) + "/public" + path), "wb") { |f| f.write(file.read) } end end when trying to upload it, I get a "cannot convert nil into String", referring to Medium.id (I tested... user.id.to_s comes out fine) once I do ''old_save'', shouldn''t Medium.id be automatically by ActiveRecord? thanks in advance, Oliver _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails Um... User.id is not set before saving the record? You''ll need to include DDL and your calling method in yoru reply to get more exacting help. Peter J. Fitzgibbons Applications Manager Lakewood Homes - "The American Dream Builder"(r) Peter.Fitzgibbons-STCS76aLmhk1y/cD6r8xzl6hYfS7NtTn@public.gmane.org (847) 884-8800
Oliver, The id _should_ automatically get set once a new record is created... not sure why it''s not in your case... but here''s a suggestion on an alternative approach. I don''t think aliasing and replacing Base#save is the right way to go here. First of all, if you really needed to modify the save method, the standard OO approach would be to define save in your subclass (Medium) and call "super" to execute the save method from the parent class (in this case, ActiveRecord::Base). But I don''t think you need to (or should) modify save. Callbacks provide a more convenient mechanism for this. If you want the uploaded file to be saved only when creating a new Meduim object, then use the after_create callback. If you want it saved when creating or modifying, use after_save. For example: class Medium < ActiveRecord::Base ... def after_save # code to save your file end end Have a look at the API documentation on callbacks for more info. Cheers, Ken
thanks ken, that made me read up on callbacks and they look very powerful. so I''m following your lead and doing this: class Medium < ActiveRecord::Base belongs_to :user after_save :save_to_filesystem attr_accessor :file attr_accessor :format attr_accessor :path def save_to_filesystem path = "/media/" + user.id.to_s + "/" + id.to_s + "." + format #code that saves the file end end but I''m still getting the same error - id is still nil : / strange isn''t it? still can''t figure out why I can''t access the created id On Aug 3, 2005, at 2:21 PM, Ken Kunz wrote:> Oliver, > > The id _should_ automatically get set once a new record is created... > not sure why it''s not in your case... but here''s a suggestion on an > alternative approach. > > I don''t think aliasing and replacing Base#save is the right way to go > here. First of all, if you really needed to modify the save method, > the standard OO approach would be to define save in your subclass > (Medium) and call "super" to execute the save method from the parent > class (in this case, ActiveRecord::Base). > > But I don''t think you need to (or should) modify save. Callbacks > provide a more convenient mechanism for this. If you want the > uploaded file to be saved only when creating a new Meduim object, then > use the after_create callback. If you want it saved when creating or > modifying, use after_save. > > For example: > > class Medium < ActiveRecord::Base > ... > def after_save > # code to save your file > end > end > > Have a look at the API documentation on callbacks for more info. > > Cheers, > Ken > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
nevermind, it was ''format'' which wasn''t getting set... dumb mistake :P tks again about the callbacks tip though, that was definetly worth the trip! tks peter also for your response On Aug 3, 2005, at 2:21 PM, Ken Kunz wrote:> Oliver, > > The id _should_ automatically get set once a new record is created... > not sure why it''s not in your case... but here''s a suggestion on an > alternative approach. > > I don''t think aliasing and replacing Base#save is the right way to go > here. First of all, if you really needed to modify the save method, > the standard OO approach would be to define save in your subclass > (Medium) and call "super" to execute the save method from the parent > class (in this case, ActiveRecord::Base). > > But I don''t think you need to (or should) modify save. Callbacks > provide a more convenient mechanism for this. If you want the > uploaded file to be saved only when creating a new Meduim object, then > use the after_create callback. If you want it saved when creating or > modifying, use after_save. > > For example: > > class Medium < ActiveRecord::Base > ... > def after_save > # code to save your file > end > end > > Have a look at the API documentation on callbacks for more info. > > Cheers, > Ken > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >