[英]How to access active_record database outside of rails project
我正在使用沒有Rails項目的active_record
數據庫。
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "posts.db")
unless File.exists? "posts.db"
ActiveRecord::Schema.define do
create_table :posts do |t|
t.column :title, :string
t.column :content, :text
end
end
end
class Post < ActiveRecord::Base
def do_something
# do_something
create(title: title, content: content)
end
end
現在,我正在使用Firefox附加SQL Lite Manager
來訪問數據庫。 但是,如果可能的話,我想像rails console
這樣訪問數據庫。
即使僅使用active_record
也可以使用rails console
嗎?
您可以安裝撬寶石。
然后在當前項目目錄中添加.pryrc 。
在您的.pryrc中,您可以添加以下內容:
current_dir = Dir.pwd $LOAD_PATH.unshift(current_dir)
現在,每次您進入撬會話時,只需手動要求您的文件即可,例如:
pry(main)> require 'models/post'
這是最簡單的方法,您可以從此處自定義或擴展。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.