在 ruby 脚本上使用 Active Record 和 mysql
我一直在尝试在 ruby 脚本上使用 mysql 和 active record,但收到以下消息:过时的 mysql gem。升级到2.8.1或更高版本。在您的 Gemfile 中:gem 'mysql', '2.8.1'。或者使用 gem 'mysql2' (RuntimeError)。在我收到有关 gem 'mysql2' 丢失的消息之前,但后来我检查了活动记录中连接适配器上的源代码,发现它需要我没有安装的 mysql gem。
这是我的脚本:
require 'rubygems'
require 'active_record'
require 'mechanize'
require 'nokogiri'
require 'active_record'
require 'mysql2'
gem 'mysql2'
ActiveRecord::Base.establish_connection ({
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "",
:database => "rainalytics"})
ActiveRecord::Schema.define(:version => 20110320035328) do
create_table "score_logs", :force => true do |t|
t.integer "blog_posts"
t.integer "featured"
end
create_table "users", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
end
class User < ActiveRecord::Base
has_many :score_logs
end
class ScoreLog < ActiveRecord::Base
belongs_to :user
end
I've been trying to use mysql and active record on a ruby script but I'm getting the following message: Outdated mysql gem. Upgrade to 2.8.1 or later. In your Gemfile: gem 'mysql', '2.8.1'. Or use gem 'mysql2' (RuntimeError). Before I was getting a message about gem 'mysql2' missing but then i checked the source code on the connection adapters in active record and saw that it require mysql gem which i didnt have installed.
this is my script:
require 'rubygems'
require 'active_record'
require 'mechanize'
require 'nokogiri'
require 'active_record'
require 'mysql2'
gem 'mysql2'
ActiveRecord::Base.establish_connection ({
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "",
:database => "rainalytics"})
ActiveRecord::Schema.define(:version => 20110320035328) do
create_table "score_logs", :force => true do |t|
t.integer "blog_posts"
t.integer "featured"
end
create_table "users", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end
end
class User < ActiveRecord::Base
has_many :score_logs
end
class ScoreLog < ActiveRecord::Base
belongs_to :user
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用 'require 'mysql2' 或 'gem 'mysql2' 部分,它似乎工作正常,我在 ActiveRecord::Base 块内还有额外的 {}
希望这可以帮助将来的人:)
这就是我使用的
ruby 1.9.2p318(2012-02-14 修订版 34678)[x86_64-darwin11.2.0]
Rails 3.1.0
然后我导航到我的应用程序文件夹并运行文件
Im not using the 'require 'mysql2' or 'gem 'mysql2' parts, and it seems to be working fine, I also have additional {} inside the ActiveRecord::Base block
hope this can help someone in future :)
This is what I use
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-darwin11.2.0]
Rails 3.1.0
then I navigate to my app folder and run the file