Hook's aren't firing
andrewpthorp opened this issue · 2 comments
andrewpthorp commented
Using:
- DataMapper - (1.0.2)
- Sinatra - (1.3.4)
- dm-is-sluggable - (0.9.10)
I have the following code:
class Post
include DataMapper::Resource
is :sluggable
property :id, Serial
property :slug, String
property :title, String, required: true, length: 255
property :body, Text, required: true
property :created_at, DateTime
property :updated_at, DateTime
before :create do
puts "Generating Slug"
set_slug(self.title.to_slug)
end
end
In my route:
post "/blog" do
@post = Post.new(params[:post])
if @post.save!
redirect "/blog"
else
haml :"posts/new", layout: true
end
end
I am not seeing "Generating Slug" in my output, and the slug is being left as nil. Alternatively, if I manually call @post.set_slug(@post.title.to_slug)
on the instance, it will write to the STDOUT and generate the slug just fine. I have tried before :create
and before :save
, neither has worked.
namelessjon commented
That's because #save!
bypasses hooks. As clearly documented here: http://datamapper.org/docs/create_and_destroy
andrewpthorp commented
Ah, thanks for that. I'm still a little too used to ActiveRecord, I suppose :)