Rails observer not being triggered on seed data
I am having an issue getting observers to trigger when using seed data in my development database.
app/models/item_observer.rb:
require 'grit'
class ItemObserver < ActiveRecord::Observer
def create(item)
Grit::Repo.init_bare(item.repository_path)
logger.debug "doneit"
end
end
db/seeds.rb:
admin_rank = UserRank.create!({
:title => 'Administrator',
:permissions => Marshal.dump(Hash.new)
})
admin = User.create!({
:username => 'admin',
:password => 'password',
:user_rank => admin_rank
})
url_operators = Item.create!({
:creator => admin,
:title => 'test game/something #1?',
:description => 'this is an example of a game entry!?\'"#<>... obviously.'
})
And in my config/application.rb i have the line:
config.active_record.observers = :item_observer
Why would the observer's create method not be triggering on rake db:seed RAILS_ENV=development? I can create the Grit开发者_如何学JAVA repository by calling Grit::Repo.init_bare from the ItemController.
(BTW, my rails version is 3.0.3).
should it not be after_create instead of just create in the observer
require 'grit'
class ItemObserver < ActiveRecord::Observer
def after_create(item)
Grit::Repo.init_bare(item.repository_path)
logger.debug "doneit"
end
end
Another reason that I found when trying to debug observers in development was that I needed to enable class cache.
config.cache_classes = true
Also make sure you have the configuration config.active_record.observers = :item_observer
set in config/development.rb file
精彩评论