How to use old gem versions with ruby? - ruby

Gem info - ruby 1.8.7, pg 0.9.0, postgres 0.8.1
All super old gems. I can't change that.
Code -
require 'rubygems'
require 'active_record'
require 'pg'
require 'postgres'
conn = PGconn.connect(:host => 'db.com', :port => 1234, :username => 'db1', :password => 'db1')
conn = PGconn.open
res = conn.exec("select * from a;")
PGconn.close
Require postgres line causes the following message and does not allow the code to execute. How do I make it work ? The warning is given below:
This is an old, deprecated version of the Ruby PostgreSQL driver that hasn't
been maintained or supported since early 2008.
You should install/require 'pg' instead.
If you need the 'postgres' gem for legacy code that can't be converted, you can
still install it using an explicit version, like so:
gem install postgres -v '0.7.9.2008.01.28'
gem uninstall postgres -v '>0.7.9.2008.01.28'
If you have any questions, the nice folks in the Google group can help:
http://goo.gl/OjOPP / ruby-pg#googlegroups.com

Related

error using sqlite3 with Ruby on Windows

I have Ruby 2.2.2p95 on Windows. I am not using Rails. I am able to use the following to install sqlite3
gem install sqlite3
When I crate a Ruby file as follows, it fails to load sqlite3:
test.rb
require 'sqlite3'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'test.db'
)
When I run
ruby test.rb
it throws an error from the line where I have the require statement for sqlite3:
cannot load such file -- sqlite3/sqlite3_native (LoadError)
I have sqlite installed on Windows and was able to create a table in the database. Any ideas on fixing this?

JDBC connectivity using ruby not jruby

Is there any gem available to Connect to JDBC DB2 for the Ruby project. I have come across activerecord-jdbc-adapter but it works only for Jruby.
Also can i use Jruby specific gem in Ruby? I have Ruby 2.2 and Jruby 9.0.0 in my machine
Did it using two other gems which was mentioned in one site.
We need Sequel and ibm_db gems and ran the below code, it worked
require 'rubygems'
require 'sequel'
require 'ibm_db'
Sequel.connect("ibmdb://#{username}:#{password}##{db_url}") do |db|
db.fetch(selectquery) do |row|
puts row
end
end

requiring old versions of gems

Since none of the tools I have handy on my Windows laptop can export decent CSV files from SQL Server to save their lives, I figured I'd roll my own in a few lines of Ruby.
Because I'm hitting the version of SQL Server from the turn of the century, I have to use an old version of activerecord-sqlserver-adapter. As far as I can tell I'm doing this correctly, and should be using activerecord ~> 2.2.3 and the 2-3-stable branch of activerecord-sqlserver-adapter, but I am getting an error that complains it is Unable to activate activerecord-sqlserver-adapter-3.2.12, because activerecord-2.2.3 conflicts with activerecord (~> 3.2.0). Here's my code (without anything specifically related to CSVs):
#!/usr/bin/env ruby
gem 'activerecord', "~> 2.2.3"
gem 'activerecord-sqlserver-adapter', github: 'arthrex/activerecord-sqlserver-adapter', :branch => '2-3-stable'
require 'activerecord'
require 'activerecord-sqlserver-adapter'
require 'pry'
ActiveRecord::Base.establish_connection(
:adapter => "sqlserver",
:mode => "odbc",
:username => "c3",
:password => "92641",
:dsn => "Connect3"
)
ActiveRecord::Base.table_name_prefix = 'dbo.'
class Dwnld_Hdr < ActiveRecord::Base
end
pry
Why is it trying to load activerecord (~> 3.2.0) in the first place?
I think in this case you might probably use bundler, because then it will allow you to run the specific gem sets.
Read here and here to get an idea on how to use bundler in a non-Rails project.

Error loading Active Record gem with sinatra app using RVM

I set up a project level RVM gemset for a sinatra app I am starting that will connect to a local database with Active Record. In order to test it I tried to run the below test app:
test.rb
require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'activerecord'
class Article < ActiveRecord::Base
end
get '/' do
Test.establish_connection(
:adapter => "sqlite3",
:database => "hw.db"
)
Test.first.content
end
(Taken from the answer to this question: What's the best way to talk to a database while using Sinatra?)
When I run ruby -rubygems test.rb I get this error:
/Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- activerecord (LoadError)
I've already installed the Active Record gem and it shows up in gem list and rvm current displays the correct gemset. I am new to RVM and I think this is something to do with the it not having the correct load path but I feel like I've set everything up correctly so I'd appreciate suggestions on what's wrong. Thanks.
As far as I can tell require 'activerecord' has been deprecated. Try using
require 'active_record'
instead.
If you have not already installed the activerecord gem, you will also get that error:
Open a command prompt and run these commands in the terminal:
#Find if the active record gem is already installed on your computer:
gem query --local
#See the downloadable gems, and see if activerecord is still available:
gem query --remote --name-matches activerecord
#Install your gem:
gem install --remote activerecord
#See if it installed successfully and is in the installed gem list:
gem query --local
Here is some code that uses the ActiveRecord gem to see if everything is configured right:
#Ruby code
require 'active_record'
class Dog < ActiveRecord::Base
has_many :dog_tags
end
puts "activerecord gem is installed";
If everything is working, it will print "activerecord gem is installed" without any errors.

How do I require a specific version of a ruby gem?

Specifically, the ruby-oci8 gem. I have both 1.0.7 and 2.0.4 installed. I want 1.0.7.
I can just require oci8, but I don't get the version I want.
irb(main):001:0> require 'oci8'
=> true
irb(main):002:0> OCI8::VERSION
=> "2.0.4"
I can require using the full path to the file, which works, but is not going to be portable:
irb(main):001:0> require 'C:\Ruby\lib\ruby\gems\1.8\gems\ruby-oci8-1.0.7-x86-mswin32-60\lib\oci8'
=> true
irb(main):002:0> OCI8::VERSION
=> "1.0.7"
I can use the gem command to ask for the version I want, but it doesn't appear to actually load the library:
irb(main):001:0> gem 'ruby-oci8', :lib=>'oci8', :version=>'=1.0.7'
=> true
irb(main):002:0> OCI8::VERSION
NameError: uninitialized constant OCI8
from (irb):2
I would definitely favor this last approach if would load the library, rather than just confirming that it's present on my system. What am I missing?
My problem was twofold:
1) confusing gem command syntax with that used in config.gem lines in a rails environment.rb configuration file.
2) failing to issue a require command after the gem command.
Proper usage in a script is:
gem 'ruby-oci8', '=1.0.7'
require 'oci8' # example is confusing; file required (oci8.rb) is not
# same name as gem, as is frequently the case
Proper usage in a rails 2.3.x environment.rb file is:
config.gem "ruby-oci8", :version=>'1.0.7'
Thanks to the folks at http://www.ruby-forum.com/topic/109100
Try the following syntax (instead of require):
require_gem 'RMagick' , '=1.10'
require_gem 'RMagick' , '>=1.10'
require_gem 'rake', '>=0.7.0', '<0.9.0'

Resources