Hi all I have the following model: class PhotoGallery < ActiveRecord::Base attr_accessible :title, :description, :file_path, :title_photo_id has_many :photos, :dependent => :destroy belongs_to :title_photo, :class_name => ''Photo'', :foreign_key => ''title_photo_id'' validates_presence_of :title, :description, :file_path def after_validation if !errors.invalid?("file_path") images = get_images if images.size == 0 errors.add_to_base("There are no files in the directory #{get_images_path}") else errors.add_to_base(images.inspect) end end rescue SystemCallError errors.add_to_base("The directory #{get_images_path} does not exist") end def after_create if errors.empty? get_images.each do |image| photos.create :file => image.basename end end raise ="asdfasdfasdfa" end # some more stuff... end When I create a new photo gallery in the console, then after_validation is being called without any problem, but after_create isn''t! I got no idea why... :-( Anyone has a clue? Thanks a lot. Joshua PS: I know, my Ruby style is still pretty bad... ;-) -- Posted via http://www.ruby-forum.com/.
Are you certain its getting created? My gut reaction is that you have a before_create that is returning false.... -hampton. On 8/14/06, Joshua Muheim <forum@josh.ch> wrote:> Hi all > > I have the following model: > > class PhotoGallery < ActiveRecord::Base > attr_accessible :title, :description, :file_path, :title_photo_id > > has_many :photos, :dependent => :destroy > belongs_to :title_photo, > :class_name => ''Photo'', > :foreign_key => ''title_photo_id'' > > validates_presence_of :title, :description, :file_path > > def after_validation > if !errors.invalid?("file_path") > images = get_images > if images.size == 0 > errors.add_to_base("There are no files in the directory > #{get_images_path}") > else > errors.add_to_base(images.inspect) > end > end > rescue SystemCallError > errors.add_to_base("The directory #{get_images_path} does not > exist") > end > > def after_create > if errors.empty? > get_images.each do |image| > photos.create :file => image.basename > end > end > raise ="asdfasdfasdfa" > end > > # some more stuff... > end > > When I create a new photo gallery in the console, then after_validation > is being called without any problem, but after_create isn''t! I got no > idea why... :-( > > Anyone has a clue? Thanks a lot. > Joshua > > PS: I know, my Ruby style is still pretty bad... ;-) > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I finally figured it out. The line if errors.empty? seems to return false, and so nothing happens. I removed the if statement, and now it works. Very strange although that the errors don''t seem to be empty...?! :O And why didn''t the raise statement have any effect...? Well, it works now, but I don''t really get it. -- Posted via http://www.ruby-forum.com/.
Seemingly Similar Threads
- STI and Inheritance
- Issue with caching of .PNG files in IE..
- ActiveRecord object.valid? triggers unexepected results
- before_create is after_validatation :on => :create , when it should be before_validation :on => :create
- difference between @secure_password, secure_password