I'm unable to index my database for searching with sunspot_rails. I get the following error:
Execute sunspot:reindex rake aborted!
undefined local variable or method
'counter'
I'm getting the following output after running rake sunspot:reindex. I'm a novice at Rails. I want to add sunspot_rails to my project to add search functionality (with the hope of deploying the project with Heroku).
I'm using Rails 3. I followed the instructions here: https://github.com/outoftime/sunspot/blob/master/sunspot_rails/README.rdoc. My various other attempts to diagnose the problem included:
installing sunspot in addition to sunspot_rails.
I ended up with sunspot_rails v. 1.2.0 and 1.2.1 so I uninstalled 1.2.1 because I have sunspot_rails 1.2.0.
installed the nokogiri gem which I understand is a dependency for sunspot_rails.
installed libxml2 separately following the instructions here to install nokogiri: http://www.engineyard.com/blog/2010/getting-started-with-nokogiri/
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sunspot:reindex rake aborted! undefined local variable or method counter' for [removed pound]<Class:0x10359aef8> /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:inmethod_missing' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:235:in solr_index' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation/batches.rb:71:infind_in_batches' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:440:in __send__' /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:440:infind_in_batches' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:234:in solr_index' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:184:insolr_reindex' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:57 /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:56:in each' /Library/Ruby/Gems/1.8/gems/sunspot_rails-1.2.0/lib/sunspot/rails/tasks.rb:56 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:incall' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:ineach' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:ininvoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:ineach' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:inrun' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:inrun' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31 /usr/bin/rake:19:in `load' /usr/bin/rake:19
This is what I have in class I'd like to search:
searchable do
text :fname
text :mname
text :lname, :default_boost => 2 end
Any help would be greatly appreciated!
The error is in the sunspot_rails-1.2.0/lib/sunspot/rails/searchable.rb:235 code.
You should fix it by yourself.
And fixing it is very easy:
def solr_index(opts={})
options = {
:batch_size => 500,
:batch_commit => true,
:include => self.sunspot_options[:include],
:first_id => 0
}.merge(opts)
if options[:batch_size]
counter = 1 #Add the variable
find_in_batches(:include => options[:include], :batch_size => options[:batch_size]) do |records|
solr_benchmark options[:batch_size], counter do
Sunspot.index(records)
end
Sunspot.commit if options[:batch_commit]
counter += 1 # Increase the variable
end
Sunspot.commit unless options[:batch_commit]
else
Sunspot.index!(all(:include => options[:include]))
end
end
Add counter variable in the front of find_in_batches, and increase it at the end of find_in_batches block.
And this variable is using for benchmark.
Don't worry~
Just to add a note, this is now fixed in Sunspot 1.2.1
That's a pretty timely intervention, user34.
Just installing Sunspot for Rails 3.0.1 running from Ubuntu 10.4. After first stumbling on connection problems with reindexing (Ubuntu users need to be sure they have Javascript running, it seems - sudo apt-get install default-jdk), I then ran into the same problem as Walter with 'counter'.
Is this a new Sunspot issue, I wonder?
Anyway, you posted your reply just a couple of hours before I needed it .. and as far as I can see, I look good to go now.
Thanks to both of you .. (and ALWAYS be wary of 5 minute installation claims :) )
Related
The following command is supposed to create a new database.
rails db:create
Where is this function defined? Or is this a pre-packaged function in rails?
It's in the databases.rake file of the framework:
namespace :create do
task all: :load_config do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
ActiveRecord::Tasks::DatabaseTasks.for_each do |spec_name|
desc "Create #{spec_name} database for current environment"
task spec_name => :load_config do
db_config = ActiveRecord::DatabaseConfigurations.config_for_env_and_spec(Rails.env, spec_name)
ActiveRecord::Tasks::DatabaseTasks.create(db_config.config)
end
end
end
Whenever you doubt or want to know where a task has been defined, you can use the rails -W (or rake) command, passing the task:
$ rails -W db:create
rails db:create /path/databases.rake:26:in `block in <top (required)>'
rails db:create:all /path/databases.rake:20:in `block (2 levels) in <top (required)>'
Note it was introduced in the version 0.9 of Rake. This might or might not work depending on the versions which you're working with.
Im having a problem with running my rails server. ive set the abstract_mysql2_adapters at initializer that consist of:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
and it solved my rake db:migrate problem, but when i try to run my rails server it gives me this error.
C:/Users/XXXX/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:2:in <class:Mysql2Adapter>': uninitialized constant ActiveRecord::ConnectionAda
pters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)
from C:/Users/John/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:1:in'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:609:in block (2 levels) in <class:Engine>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:ineach'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:in block in <class:Engine>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:ininstance_exec'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:inblock in run_initializers'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in block in tsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:inblock (2 levels) in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in each_strongly_connected_component_from'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:inblock in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:incall'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:intsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in tsort_each'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:inrun_initializers'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/application.rb:215:in initialize!'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:inmethod_missing'
from C:/Users/John/Documents/RoRCmS/simple_cms/config/environment.rb:5:in <top (required)>'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:inrequire'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:in block in <main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:ininstance_eval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in initialize'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:innew'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:in <main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:ineval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:in new_from_string'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:40:inparse_file'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:277:in build_app_and_options_from_config'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:199:inapp'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:in app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:314:inwrapped_app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:in start'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:78:inblock in '
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in tap'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in'
from bin/rails:4:in require'
from bin/rails:4:in'
I really need to solve this problem now.. thanks guys.
I solved it as follows. After importing a legacy sql dumpfile into MySQL with mysql -u username -p database_name < file.sql, I proceeded to perform a migration with bin/rake db:migrate, but I encountered the error Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead.
So I tried the same monkey patch as you, as suggested by user pjg here https://github.com/rails/rails/pull/13247#issuecomment-32425844, but then I encountered your error
uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)
I followed user John Geliberte's advice and updated my Gemfile with gem 'activerecord-native_db_types_override' and checked that I also had gem 'mysql2' (NOT 'activerecord-mysql2-adapter' which causes other errors) and ran bundle install.
I used the the documentation associated with the activerecord-native_db_types_override gem and applied property for NATIVE_DATABASE_TYPES mentioned in Rails ActiveRecord library file for abstract_mysql_adapter, such that I made the following changes:
I created config/initializers/abstract_mysql2_adapter.rb and added:
require 'active_record/connection_adapters/mysql2_adapter'
NativeDbTypesOverride.configure({
ActiveRecord::ConnectionAdapters::Mysql2Adapter => {
primary_key: "int(11) auto_increment PRIMARY KEY"
}
})
I created config/environment.rb and add the following to load the monkey patch:
require File.expand_path('../initializers/abstract_mysql2_adapter.rb', __FILE__)
I checked that config/database.yml contained 'adapter: mysql2' for the db connection I wanted to use.
I then ran bin/rake db:migrate and it successfully performed the migration.
I have made a pull request to the documentation for the ActiveRecord Native Database Types Override Gem.
It was much easier to downgrade from MySQL 5.7 to 5.5 instead though.
My system:
mysql -V is mysql Ver 14.14 Distrib 5.7.13
rails -v is 4.2.4
ruby -v is ruby 2.3.0p0
SOLVED: i added the gem : gem 'activerecord-native_db_types_override'
Try this code instead:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
def modify_types(types)
super
types[:primary_key] = "int(11) auto_increment PRIMARY KEY"
types
end
end
I'm trying to create an xml runner to make a result report of Rails unit testing. Here is a code I have:
require 'test/unit'
require 'test/unit/ui/console/testrunner'
class FastFailRunner < Test::Unit::UI::Console::TestRunner
def add_fault(fault)
#faults << fault
nl
output("%3d) %s" % [#faults.length, fault.long_display])
output("--")
#already_outputted = true
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
nl
output(#result)
end
end
Test::Unit::AutoRunner::RUNNERS[:fastfail] = proc do |r|
FastFailRunner
end
When I run it as TESTOPTS="/home/alex/RubymineProjects/app2/test/unit/runner.rb --runner=xml" rake test
... I get an error (pretty weird error)
/home/alex/.rvm/gems/ruby-1.9.3-p194#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
gem install minitest
/home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/runner/xml.rb:5:in `block in <module:Unit>': uninitialized constant Test::Unit::UI::XML (NameError)
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `[]'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:389:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit/autorunner.rb:58:in `run'
from /home/alex/.rvm/gems/ruby-1.9.3-p194/gems/test-unit-2.5.2/lib/test/unit.rb:501:in `block in <top (required)>'
Errors running test:units! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
Errors running test:functionals! #<RuntimeError: Command failed with status (1): [/home/alex/.rvm/rubies/ruby-1.9.3-p194/bin...]>
I tried to require test/unit/ui/xml/testrunner.rb but no luck.
Any thoughts?
If you are in Ruby 1.8.7 you can say:
require 'test/unit/ui/console/testrunner'
p Test::Unit::UI::Console::TestRunner # => no problem
(There is no such file as test/unit/ui/xml/testrunner.rb so I'm not sure what you were up to there.)
Look in the docs in test/unit.rb, there's actually sample code showing you how to do this require: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit.html.
However, the problem is that you are in Ruby 1.9.3. There is no /test/unit in Ruby 1.9.3! Well, there is, but it's just a compatibility layer for basic tests; there is certainly no test/unit/ui/console/testrunner, and no module/class Test::Unit::UI::Console::TestRunner.
Instead, there's minitest. You can read the docs on minitest to see how to make a test runner. http://docs.seattlerb.org/minitest/
One thing to consider is that Test::Unit was included in the default Ruby 1.8.7 installation. If you want to use it in with a later version of Ruby then go ahead and install it as a gem.
sudo gem install test-unit
http://test-unit.rubyforge.org/
I'm struggling with this error after I fire the command rake apn:notifications:deliver on the APN on Rails gem that I've installed.
It's barking about the RAILS_ENV variable. I've tried a couple of forks that change RAILS_ENV to Rails.env but I still get the same error. I've posted my issue over on that repo hoping I might get somewhere.
I don't know enough about rails to dig in any further. I'm hoping someone could point me in the right direction so that I can a) better understand what went wrong and b) fix the problem.
I am using bundler and I'm pointing my gem file to the git repo: rake apn:notifications:deliver
air:apnapp azcoov$ rake apn:notifications:deliver --trace
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/version.rb:2: warning: already initialized constant VERSION
** Invoke apn:notifications:deliver (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute apn:notifications:deliver
rake aborted!
uninitialized constant APN::App::RAILS_ENV
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:11:in `cert'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:22:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:32:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `each'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/app/models/apn/app.rb:31:in `send_notifications'
/Users/azcoov/.bundler/ruby/1.8/apn_on_rails-ca98c7c130f0/lib/apn_on_rails/rails/../tasks/apn.rake:7
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
Tasks: TOP => apn:notifications:deliver
Which version of Rails to you use? I just ran into this issue too. My Rails knowledge is not very deep, but it seems like the problem is deprecation of RAILS_* environment variables. I use Rails 3.1.0 and I do NOT have:
RAILS_ROOT/ENV/blah
in my environment. Instead, I have:
Rails.root/env/blah
When I replace RAILS_ENV with Rails.env in app/models/apn/app.rb the problem goes away.
So, is this gem compatible with Rails 3.1.0??? It seems not. Is there a workaround that will allow its use in a Production environment? Maybe define somewhere:
APN::App::RAILS_ENV = Rails.env
But where?
UPDATE:
I got it working with a workaround described by garyfoster here: https://github.com/PRX/apn_on_rails/issues/53.
To summarize, three things:
1) Add this ghetto hack to your config/environment.rb file:
APN::App::RAILS_ENV = Rails.env
2) Insert your certs in the APN::App table. I wrote a one-time rake task for that:
task :init_apn_certs => [:environment] do # NOTE: One-time task...
print "Creating APN::App with certs..."
app = APN::App.create(:apn_dev_cert => Rails.root.join('config', 'apple_push_notification_development.pem').read,
:apn_prod_cert => Rails.root.join('config', 'apple_push_notification_production.pem').read)
puts (app.valid? ? "done" : "failed")
end
3) Write your own rake task that bypasses the APN::App.send_notifications method, as that barfs with another error (something about '/config/apple_push_notification_development.pem' not existing). My code for it:
task :deliver_notifications => [:environment] do
print "Delivering APNs for app ids..."
apps = APN::App.all
apps.each do |app|
app.send_notifications
print app.id.to_s + "..."
end
puts "done."
end
Or, if you have only one app, you can just do this and be done with it:
task :deliver_notifications => [:environment] do
APN::App.first.send_notifications
end
DISCLAIMER: I'm yet to test this on Staging and Production (will do so by the end of the week), but I see no reason why it shouldn't work.
I have a very annoying problem with my migrations.
First the Errormessage:
bundle exec rake db:migrate --trace
(in /home/myhomefolder/msdnaa)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
An error has occurred, all later migrations canceled:
uninitialized constant Computers
Now i have the assumption that an Update made by one of our admins is the reason why this happens every time. Even if there are no migrations at all!
What i've done (besides searching on Stack Overflow for a solution) is to grep every single file for "Computers".
Of course there are some Files containing this word and i checked them for Syntax Errors and the usual Stuff like missing ":".
Then i asked a workmate for some help (he is much more skilled with ruby than i am) and he hasn't a clue, everything looks right.
I'm using a slightly old Version of Ruby (1.8.7) and Rails (3.0.9), but i have no rights to do an update on our Server, so i have to deal with it.
And yes i asked the admin to do an update to 1.9.x and 3.1.x, but that can't be the fault 'cause it worked fine last week.
So it's one of those Errors where it SHOULD work, but it doesn't and i bet the solution is easy as drinking water, but i don't see it!
Any suggestions?
EDIT: Here ist the --trace:
/var/lib/gems/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/meth ods.rb:113:in `constantize'
/var/lib/gems/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/meth ods.rb:112:in `each'
/var/lib/gems/1.8/gems/activesupport-3.0.9/lib/active_support/inflector/meth ods.rb:112:in `constantize'
/var/lib/gems/1.8/gems/activesupport-3.0.9/lib/active_support/core_ext/strin g/inflections.rb:43:in `constantize'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:407 :in `load_migration'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:402 :in `migration'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:397 :in `migrate'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:539 :in `migrate'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:615 :in `call'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:615 :in `ddl_transaction'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:538 :in `migrate'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:525 :in `each'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:525 :in `migrate'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:435 :in `up'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/migration.rb:417 :in `migrate'
/var/lib/gems/1.8/gems/activerecord-3.0.9/lib/active_record/railties/databas es.rake:142
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in `execute'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:158:in `invoke_with_call_ chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_ chain'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_tas k'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `top_level'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `top_level'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_e xception_handling'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:62:in `run'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_e xception_handling'
/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/var/lib/gems/1.8/gems/rake-0.9.2/bin/rake:32
/var/lib/gems/1.8/bin/rake:19:in `load'
/var/lib/gems/1.8/bin/rake:19
Tasks: TOP => db:migrate
My guess is that you have just added a migration called something like 20120221123456_computers.rb, and within it, you have something like...
class SomeNameThatsNotComputers < ActiveRecord::Migration
...
When Rails executes a migration, it expects to execute the file, which defines the class, and then instantiate the class, and call #up or #down on that class instance. How does Rails know what class to instantiate? It's supposed to correspond with the part of the file name following the numeric prefix and underscore, so for a file name like 20120221123456_computers.rb, the class name must be Computers.
As you said grep tells existence of "Computers" that's the problem. It means Computers is not defined but getting used. migration loads the app environment first and that time it fails bcoz Computers is not initialized