在脚本中使用不带 Rails 的 ActiveRecord 3.1
以下应该在ActiveRecord 3.1中作为脚本工作(请注意,这是没有Rails的,而不是相反):
#!/usr/bin/env ruby
require "rubygems"
require "active_record"
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
irb
不幸的是,它给出了错误:
... connection_specification.rb:71:in `rescue in establish_connection': ...
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` ...
添加按照之前的一些帖子的建议,在 require "active_record"
之前添加行 gem 'mysql2', '<0.3'
时间;目前是 0.2.18) 不会改变它。
我怎样才能让它发挥作用?我想要 ActiveRecord,但不需要整个 Rails。
The following ought to work as a script, with ActiveRecord 3.1 (note that this is without Rails, not the other way around):
#!/usr/bin/env ruby
require "rubygems"
require "active_record"
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
irb
Unfortunately, it gives the error:
... connection_specification.rb:71:in `rescue in establish_connection': ...
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` ...
Adding the line gem 'mysql2', '<0.3'
before require "active_record"
as suggested by some previous posts (which reference 0.2.7, the 0.2 gem at the time; presently it's 0.2.18) doesn't change it.
How can I get it to work? I want ActiveRecord but not the whole of Rails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在终端中运行
并向您的代码添加行
require 'mysql2'
Run in terminal
and add row to you code
require 'mysql2'
这修复了我的问题问题,至少是暂时的。我还没有重新启动,所以我不知道它是否可以幸存。这看起来像是一个肮脏的黑客行为;一定有更好的方法。
我还没有测试上面 Aleksei 的答案,因为运行这个命令后我没有遇到同样的问题。当我这样做时会更新。
This has fixed my issue, at least temporarily. I haven't yet restarted, so I don't know if it'll survive that. And it seems like a dirty hack; there's got to be a better way.
I haven't yet tested Aleksei's answer above, since I'm not having the same issue after having run this command. Will update ifwhen I do.