# After\_create is not being called

**URL:** <https://www.ruby-forum.com/t/after-create-is-not-being-called/102284>\
**Category:** Rails\
**Created:** [August 14, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/after-create-is-not-being-called/102284 "2006-08-14T18:52:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 14, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/after-create-is-not-being-called/102284/1 "2006-08-14T18:52:45Z")

</div>

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… 😉

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/after-create-is-not-being-called/102284/2 "2006-08-15T14:20:36Z")

</div>

Are you certain its getting created?

My gut reaction is that you have a before\_create that is returning  
false…

-hampton.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 15, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/after-create-is-not-being-called/102284/3 "2006-08-15T16:27:19Z")

</div>

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…?! 😮

And why didn’t the raise statement have any effect…?

Well, it works now, but I don’t really get it.
