需要帮助来设置带有ActiveRecord的数据库应用程序,而无需使用Rails。到目前为止,我已经阅读了文档,并且在安装pg gem
时遇到错误。这是在我的环境中。rb。
require 'bundler/setup'
Bundler.require(:default, ENV['SINATRA_ENV'])
require 'active_record'
ActiveRecord::Base.establish_connection("postgres://localhost/development")
require_all 'app'
每次我使用sudo ARCHFLAGS="-arch x86_64" gem install pg
在CLI上安装postgres。我收到以下错误:
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/pg-1.1.4/ext
/Users/kenkuts/.rvm/rubies/ruby-2.5.3/bin/ruby -r ./siteconf20190206-73835-1tfhfez.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/kenkuts/.rvm/rubies/ruby-2.5.3/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include
--without-pg-include=${pg-dir}/include
--with-pg-lib
--without-pg-lib=${pg-dir}/lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0/pg-1.1.4/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/gems/pg-1.1.4 for inspection.
Results logged to /Users/kenkuts/.rvm/rubies/ruby-2.5.3/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0/pg-1.1.4/gem_make.out
答案 0 :(得分:0)
按如下所示先安装postgreSQL
开发软件包,
sudo apt-get install libpq-dev
或尝试
gem install pg -- --with-pg-lib=/usr/lib
然后将active_record
与postgres
一起使用的最常见方式如下,
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
# database schema i.e. database, encoding, username, password etc.
)
然后您可以按如下所示定义类,
class Post < ActiveRecord::Base
end