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.
Related
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
I am using bundler to require all the gems in my project. However, it's not working for yaml/logger.
If I add gem 'yaml' to my gemfile, and run bundle install, I get:
Could not find gem 'yaml (>= 0) ruby' in the gems available on this machine.
But I require it normally just fine. What am I doing wrong?
Thanks
YAML is part of the Ruby Standard Library, and not a Gem.
You do not need to add it to your Gemfile, just require it.
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML
=> Psych
The same applies for Logger.
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.
I am trying to use paperclip without rails(but trying to connect db created by rails).
Using bundler to require gems.
here are my models
class RailsDB < ActiveRecord::Base
establish_connection $db_config[:rails_db]
end
class VoiceCall < RailsDB
belongs_to :campaign
set_table_name :voice_calls
has_attached_file :sound_file
validates_attachment_presence :sound_file
end
If I try to run the program it throws the error
undefined method `has_attached_file
any idea y?
Edit:
my gem file
source "http://rubygems.org"
gem 'activerecord', '< 3.1', :require => 'active_record'
gem 'mysql2', '< 0.3'
gem "paperclip", "~> 2.4"
I require gems using
require "bundler/setup"
Bundler.require(:default)
One more observation. I started irb and required active record and then paperclip. and ran this
p ActiveRecord::Base.methods.select{|m| m =~ /has_attached_file/}
It returns empty list.
but when I open rails console (using "rails c") the statement works and returns the value.( Both are using same gems)
This error means the Paperclip gem is not loaded correctly (or at all) within your application.
Can you post your Gemfile and config/preinitializer.rb?
Is bundler working to successfully load other Gems in your environment?
I've seen this happen when Bundler was not configured correctly for a Rails project causing gems to fail to load. The paperclip model references are the canary in the coal-mine for this larger problem.
had the same issue.
using
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
instead helped out.
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'