开发者

Using AR in external script works unless called by system() from Rails

I'm calling an external script from rails with system() and I get this:

/home/user/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-
3.0.7/lib/active_record/connection_adapters/mysql_adapter.rb:22:in
`rescue in mysql_connection': !!! Missing the mysql2 gem. Add it to
your Gemfile: gem 'mysql2' (RuntimeError)
...

This error is being caused by the external script, which loads rubygems and active record without rails. The script still works from the shell if I specify the same version of mysql2 rails is using:

require 'rubygems'
gem 'mysql2', '0.2.7'
require 'active_record'
...#active record initialization
c = Class.new # Class uses rails models
c.run

I'm also able to use the class the script uses within rails. This means I could use rake to run the script after stripping the active_record initialization. However it would be nice to know what's causing this first.

Edit. Running the script with bundle exec raises the same error unless I uncomment require 'active_record'. In this case I'm left with NameErrors when I invoke the models.

Edit2. Fixed by diffing 'gem list' and 'bundle exec gem list' and removing versions of gems newer than bundler's versions. Running without bundle exec also still works, which I didn't expect. In the Gemfile I was requiring rails 3.0.7, but 3.1.0.beta1 version开发者_运维知识库s of activerecord were also installed. It looks like bundle exec ignores the rails version and loads the newest version of activerecord. Perhaps adding gem 'activerecord', '3.0.7' to the external script or the gemfile would also fix this issue.


Since you're using RVM, it's possible that you're running in a different Ruby that isn't acting within the same context as your bundle. Make sure you're using bundle exec to run this command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜