在 ruby​​ 脚本上使用 Active Record 和 mysql

发布于 2024-10-26 15:55:32 字数 1045 浏览 1 评论 0原文

我一直在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2024-11-02 15:55:33

我没有使用 'require 'mysql2' 或 'gem 'mysql2' 部分,它似乎工作正常,我在 ActiveRecord::Base 块内还有额外的 {}

希望这可以帮助将来的人:)

这就是我使用的
ruby 1.9.2p318(2012-02-14 修订版 34678)[x86_64-darwin11.2.0]
Rails 3.1.0


require "rubygems"
require "active_record"

ActiveRecord::Base.establish_connection ({
  :adapter => "mysql2",
  :host => "localhost",
  :username => "root",
  :password => "root",
  :database => "cybercellar"})

class Zone < ActiveRecord::Base

end

zone = Zone.all
print zone

然后我导航到我的应用程序文件夹并运行文件

Ruby 脚本/test.rb

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


require "rubygems"
require "active_record"

ActiveRecord::Base.establish_connection ({
  :adapter => "mysql2",
  :host => "localhost",
  :username => "root",
  :password => "root",
  :database => "cybercellar"})

class Zone < ActiveRecord::Base

end

zone = Zone.all
print zone

then I navigate to my app folder and run the file

ruby script/test.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文