Timeout with ActiveRecord(ActiveRecord::ConnectionTimeoutErr



在使用ActivereCord与Sinatra创建应用程序时,我遇到了这样的错误。

activerecord :: connectionTimeouterror在/无法获得数据库 连接在5.000秒内(等待5.005秒)

我要做的是"从Twitter API获取配置文件,将其保存在数据库中,然后将其嵌入index.erb中"。我在这里停了下来。

代码在下面。

main.rb

require "sinatra"
require "sinatra/reloader"
require "twitter"
require "active_record"

ActiveRecord::Base.establish_connection(
    adapter: "sqlite3",
    database: "./image.db"
)   
class Src < ActiveRecord::Base
end
def get_profile_image
    @srcs = Src.order("id desc").all
end
def add_profile_image(twitter_id)
    client = Twitter::REST::Client.new(
        consumer_key: "[CONSUMER_KEY]",
        consumer_secret: "[CONSUMER_SECRET]",
        access_token: "[ACCESS_TOMEN]",
        access_token_secret: "[ACCESS_TOKEN_SECRET]"
    )
    user = client.user(twitter_id)
    url = user.profile_image_url
    src = Src.new(account: twitter_id, url: url)
    src.save 
end
def delete_img(twitter_id)
    Src.find_by(account: twitter_id).delete
end

get "/" do
    get_profile_image
    erb :index
end
post "/" do
    add_profile_image(params[:id])
    get_profile_image
    erb :index
end
get "/delete" do
    delete_img(params[:id])
    get_profile_image
    erb :index
end

index.erb

  <form method="post" action="/">
    <input type="text" name="id">
    <input type="submit" value="enter">
  </form>
  <form method="get" action="/delete">
    <input type="text" name="id">
    <input type="submit" value="delete">
  </form>
  <% @srcs.each do |src| %>
    <div id="images-space" style="float: left;" class="imgs">
      <img data-id="<%= src.id %>" src="<%= src.url %>">
    </div>
  <% end %>

db表名是" srcs",列为

  • ID Integer主键
  • 帐户文本
  • URL文本

这种错误是我第一次,但是问题在哪里?

在单独的脚本中将连接到数据库的连接将其与产生的错误进行测试和发布,以便我们可以重现问题,例如

require "active_record"
ActiveRecord::Base.establish_connection(
    adapter: "sqlite3",
    database: "./image.db"
) 
if !ActiveRecord::Base.connection.table_exists?('SRCS')
  ActiveRecord::Schema.define do
    create_table 'SRCS' do |table|
      table.column :account, :string
      table.column :url, :string
    end
  end
end
class Src < ActiveRecord::Base
  self.table_name  = 'SRCS'
end
p Src.first

相关内容

  • 没有找到相关文章

最新更新