#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Trouble with Callbacks


    Hi,

    I'm still new to Ruby on Rails, and I want to utilize ActiveRecord::Observer. I got Observer working under the rails environment by putting my rb files under app/model and configuring config/environment.rb. However, I want to automate the testing of my site via command line. Is this possible?

    So, I have a foo.rb
    Code:
    (bchin ~/setup/rails/pratice/app/models)cat foo.rb 
    class Foo < ActiveRecord::Base
    end
    and my foo_observer.rb
    Code:
    (bchin ~/setup/rails/pratice/app/models)cat foo_observer.rb 
    class FooObserver < ActiveRecord::Observer
    
      def after_create(foo)
        puts foo.id
        Foo.find(foo.id)
      end
    
    end
    and I registered foo_observer under config/environment
    Code:
    (bchin ~/setup/rails/pratice/app/models)grep 'foo' ../../config/environment.rb 
      config.active_record.observers = :foo_observer
    And, when I go to the console, everything works as expected
    Code:
    (bchin ~/setup/rails/pratice/script)./console 
    Loading development environment (Rails 2.3.2)
    >> Foo.create
    2
    => #<Foo id: 2>
    >>
    How can I use ruby to test this? I tried creating a main.rb, which 'require' foo and foo_observer, but that doesn't seem to work. What am I missing? Thanks for any help anyone can provide.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0
    well, looks like I can just following this guide, http://guides.rubyonrails.org/testing.html to get my automated testing done. So I followed the guides and re-created all my objects using 'rails generate', but now I'm getting the following error:
    Code:
    (bchin ~/setup/rails/practice)rails console
    /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/application.rb:63:in `inherited': You cannot have more than one Rails::Application (RuntimeError)
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/deprecation.rb:7:in `initialize'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/deprecation.rb:7:in `new'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/deprecation.rb:7:in `run'
    	from /home/bchin/setup/rails/ioffer_cl/config/environment.rb:7:in `<top (required)>'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/application.rb:103:in `require'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/application.rb:103:in `require_environment!'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:22:in `<top (required)>'
    	from script/rails:6:in `require'
    	from script/rails:6:in `<main>'
    What does this error mean?

IMN logo majestic logo threadwatch logo seochat tools logo