(Ruby) ActiveRecord with PG try to connect with local user to remote server - ruby

I made a simple-coding-ruby-program for get backups from remote server, and I made a db in postgresql for save information and schedule backups.
The connect with DB was done with ActiveRecord, I configured for access a internal DB (in other server), but when I try to connect to remote db, i get this message:
/usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `initialize': FATAL: no existe el rol <<mi_user_name>> (PGError)
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:217:in `initialize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `new'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
from /usr/lib/ruby/1.8/active_record/base.rb:3113:in `quoted_table_name'
from /usr/lib/ruby/1.8/active_record/base.rb:1684:in `construct_finder_sql'
from /usr/lib/ruby/1.8/active_record/base.rb:1548:in `find_every'
from /usr/lib/ruby/1.8/active_record/base.rb:1505:in `find_initial'
from /usr/lib/ruby/1.8/active_record/base.rb:613:in `find'
from main.rb:84:in `main'
from main.rb:126
PG try to connect to remote db with my local user, in the declaration of ActiveRecord I set the parameters:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:user => "pg_user",
:password => "blahblah"
)
I working with Ruby 1.8.7.
Any idea about this?
thank for reading, and help me.
Grettings.
EDIT
I found the solution:
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:username => "pg_user",
:password => "blahblah"
)
I used :user and must be :username.
Shame on me.

From the error message, it appears that Postgres doesn't think your user exists in the database.
A good first step to troubleshoot this is to try connecting without involving Ruby at all. Since the 'psql' command-line client uses the same underlying library (assuming you're using the 'pg' gem to connect), in general if you can connect using that, you should be okay to connect from Ruby as well.
Try this:
psql -h xxx.xxx.x.101 -U pg_user -W -l
If this shows you the same error (no existe el rol <<mi_user_name>>), then you'll need to confirm that your user exists, and create it.
If it shows you a list of databases, we'll have to try something else.

Related

ruby-oci8: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

I tried to connect to Oracle DB with the latest oracle_enhanced, ruby-oci8 gems on a Windows 7 x64 PC and it failed as follows:
$ ruby oracle_db_connection.rb
oci8.c:654:in oci8lib_230.so: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor (OCIError)
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/ruby-oci8-2.2.2-x64-mingw32/lib/oci8/oci8.rb:142:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:393:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `create'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:156:in `oracle_enhanced_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/model_schema.rb:230:in `table_exists?'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
from C:in `primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:493:in `find_nth_with_limit'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:484:in `find_nth'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:127:in `first'
from C:in `first'
from oracle_db_connection.rb:30:in `<main>'
I installed Oracle instant client by downloading and unzipped it to
**C:\Program Files\Oracle\instantclient_12_1**
I added the environment variable TNS_ADMIN pointing to C:\Program Files\Oracle\instantclient_12_1.
I added TNS_ADMIN to the Path.
When running the simple ruby script:
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
class Customer < ActiveRecord::Base
self.table_name = 'CUSTOMER'
end
puts "First: #{Customer.first}"
If I remove the call to the Customer table, I can inspect connection object:
connection = ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
puts connection.inspect
#class Customer < ActiveRecord::Base
# self.table_name = 'CUSTOMER'
#end
#
#puts "First: #{Customer.first}"
Any idea on what is wrong here ? The script worked previousely fine on other PCs but with other DBs and environement. So the used settings should be fine.
Thank you.
Environment:
Windows 7 x 64 bits Ruby 2.3
activerecord-oracle_enhanced-adapter (1.6.7)
ruby-oci8 (2.2.2 x64-mingw32)
You need to use some different format of database section. Here's my config:
adapter: "oracle_enhanced"
username: "db_user"
database: "//my.host/my.sid"
password: "passw0rd"
I figured out how to connect to one of 2 databases. They have different settings, at least in Sqldeveloper v 4.1.3.20.
Here are the settings that worked for me (please note, it is out of Rails context, just a ruby file):
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
host: 'as specified in the field Host name of SQL developer',
port: 'as specified in the field Port of SQL developer',
database: 'as specified in the fiels Service name of SQL developer',
username: 'username',
password: 'userpassword'
)
class Department < ActiveRecord::Base
self.table_name = 'DEPARTMENTS'
end
puts "First found department: #{Department.first.inspect}"
The second database has different settings in SQL developer:
- host: some_host_url
- port: some port number
- SID: some SID value
The last soltion was to add the following settings:
HOST = "your host"
PORT = XXXX #your port number
SID = 'your SID'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: "(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=#{HOST})(PORT=#{PORT})))
(CONNECT_DATA=(SID=#{SID}))
)",
username: 'user',
password: 'secret'
)
Hope this helps.

Connect to existing Postgresql database with ActiveRecord without rails

I am not using rails, and I'm trying to use ActiveRecord to make it easier to work with an existing database. This is just a script so I have no database.yml or any other files. I've set up my database connection using
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: '5432'
)
I'm not really familiar with databases, so I'll just state what I know about this one. There are quite a few schemas. The schema I'm interested in is the management schema that has the table "host". I've created a class Host in the script like this:
class Host < ActiveRecord::Base
self.table_name = "host"
end
I'm then trying to pull all rows that match the criteria with this query and to store it into an array.
servers = Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)
but I am getting this error every single time.
ruby environments_are_okDev.rb
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec': PG::UndefinedTable: ERROR: relation "host" does not exist (ActiveRecord::StatementInvalid)
LINE 5: WHERE a.attrelid = '"host"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid,
a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"host"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `column_definitions'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:174:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `default'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:208:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:247:in `column_names'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `block in method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:152:in `block in pluck'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `map!'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `pluck'
from environments_are_okDev.rb:51:in `run'
from environments_are_okDev.rb:97:in `<main>'
I don't really understand why this is happening, so I tried to see what the database looks like with this command ActiveRecord::Base.connection.tables but all I got was an empty array. I'm not sure what could be happening. I'm obviously doing something wrong, I just can't figure out what. Keep in mind, this is all in a single file script.
Edit:
So I can see the schemas if I use ActiveRecord::Base.connection.schema_names. This gives me an array => ["db_stats", "management", "management_audit", "public"] Using pgadmin, I know that the table I want is in management, so how do I access that?
Try adding schema_search_path when you call establish_connection:
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: 5432,
schema_search_path: 'management' # <-- ???
)

Need to implement Watirgrid,

I want to implement watirgrid, but I'm not able to do that, every time I'm getting errors related with controller and provider starting process, Also all the example over internet, none of them are working.
Could any one please help me to implement this, a full running example with steps will be a great help.
I'm trying this code:
require 'rubygems'
require 'watirgrid'
require 'watir'
require 'watir-webdriver'
# setup a controller on port 12351 for your new grid
controller = Controller.new(:ring_server_port => 12351, :loglevel => Logger::ERROR)
controller.start
# add a provider to your grid
# :browser_type => 'webdriver' if using webdriver or
# :browser_type => 'ie' if using watir...
provider = Provider.new(:controller_uri => 'druby://127.0.0.1:11235',
:ring_server_port => 12351,
:loglevel => Logger::ERROR,
:browser_type => 'webdriver')
provider.start
# connect to the grid and take all providers from it (this time only one)
grid = Watir::Grid.new(:ring_server_port => 12351, :ring_server_host => '127.0.0.1')
grid.start(:take_all => true)
# for each provider on the grid, launch a new thread to start multiple browsers
threads = []
grid.browsers.each do |browser|
threads << Thread.new do
p browser[:hostname]
p browser[:architecture]
p browser[:browser_type]
# in this case we are starting a new IE browser
b = browser[:object].new_browser(:ie)
b.goto("http://www.google.com")
b.text_field(:name, 'q').set("watirgrid")
b.button(:name, "btnI").click
end
end
threads.each {|thread| thread.join}
And Errors I'm getting is
DRb::DRbConnError: druby://127.0.0.1:11235 - #<Errno::ECONNREFUSED: No connection could
be made because the target machine actively refused it. - connect(2)>
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:736:in `rescue in block in open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:730:in `block in open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:729:in `each'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:729:in `open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1191:in `initialize'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1171:in `new'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1171:in `open'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1087:in `block in method_missing'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1105:in `with_friend'
from C:/Ruby192/lib/ruby/1.9.1/drb/drb.rb:1086:in `method_missing'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watirgrid-1.1.5/lib/provider.rb:141:in `start'
from (irb):44
from C:/Ruby192/bin/irb:12:in `<main>'
irb(main):045:0>
I got the same problem, I got the provider started successfully by changing the
controller_uri
to
controller_uri => 'druby://machineIPAddress:11235'

Using mechanize (or even Net::HTTP) with ruby 1.9 returns 'OpenSSL::SSL::SSLError' - how to force SSLv3?

For the past 2 days, I think I've been through every single available (google'able) post about this SSL-Error in connection with Net::HTTP:
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
What I tried doing in the first place, was connecting to a EtherPad server (https://test.titanpad.com), signing in, and downloading a zip-archive via using rubygems & mechanize; however, unfortunately I don't even get up to that point due to said SSL-Error. After trying to debug the issue from within a mechanize object (f.e. by setting cert, ca_file, cert_store, verify_mode, etc. manually in the script), I went one level closer to the actual issue, trying to connect to https://test.titanpad.com by simply using Net::HTTP:
(in this example, I first connected to https://encrypted.google.com to make sure, SSL should work out; the attempt to connect to the EtherPad server starts from line 6)
irb(main):001:0> require 'net/https'
=> true
irb(main):002:0> google = Net::HTTP.new('encrypted.google.com', 443)
=> #<Net::HTTP encrypted.google.com:443 open=false>
irb(main):003:0> google.use_ssl = true
=> true
irb(main):004:0> google.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):005:0> google.request_get('/')
=> #<Net::HTTPOK 200 OK readbody=true>
irb(main):006:0> etherpad = Net::HTTP.new('test.titanpad.com', 443)
=> #<Net::HTTP test.titanpad.com:443 open=false>
irb(main):007:0> etherpad.use_ssl = true
=> true
irb(main):008:0> etherpad.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):009:0> etherpad.request_get('/')
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `block in connect'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:54:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:99:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:755:in `do_start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:744:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1284:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1195:in `request_get'
from (irb):9
from /opt/local/bin/irb:12:in `<main>'
Even when using verify_mode OpenSSL::SSL::VERIFY_NONE, OpenSSL bails out:
irb(main):010:0> etherpad.verify_mode = OpenSSL::SSL::VERIFY_NONE
=> 0
irb(main):011:0> etherpad.request_get('/')
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `block in connect'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:54:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/timeout.rb:99:in `timeout'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:799:in `connect'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:755:in `do_start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:744:in `start'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1284:in `request'
from /opt/local/lib/ruby1.9/1.9.1/net/http.rb:1195:in `request_get'
from (irb):11
from /opt/local/bin/irb:12:in `<main>'
After further playing around with openssl itself, it turns out the real trouble in this case is that the use of SSLv3 has to be forced for the handshake with the Jetty 6.1.20 server behind titanpad.com to work:
irb(main):001:0> require 'net/https'
=> true
irb(main):002:0> etherpad = Net::HTTP.new('test.titanpad.com', 443)
=> #<Net::HTTP test.titanpad.com:443 open=false>
irb(main):003:0> etherpad.use_ssl = true
=> true
irb(main):004:0> etherpad.ssl_version = "SSLv3"
=> "SSLv3"
irb(main):005:0> etherpad.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?('/opt/local/share/curl/curl-ca-bundle.crt')
=> "/opt/local/share/curl/curl-ca-bundle.crt"
irb(main):006:0> etherpad.request_get('/')
=> #<Net::HTTPFound 302 Found readbody=true>
Now while this obviously works when using Net::HTTP, there is no such option as to set the SSL version to use in Mechanize... and I'd therefore be really glad if someone could point out to me as to how I could enforce SSLv3 via said gem o.O
Thanks again!
System: Mac OSX 10.6.8
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10]
rubygems installed with mechanize: domain_name (0.5.2), mechanize (2.1.1), net-http-digest_auth (1.2), net-http-persistent (2.4.1), nokogiri (1.5.0), ntlm-http (0.1.1), unf (0.0.4), unf_ext (0.0.4), webrobots (0.0.13)
Has been fixed by porting the ssl_version functionality from Net::HTTP (via net-http-persistent) to Mechanize v. 2.1.2 (see https://github.com/tenderlove/mechanize/commit/4a228899855e0676ab69c2bf548170c8717465d8).

Do I need a connection to use ActiveRecordObject.new?

Can you create and initialize an ActiveRecord object like this?
exercise = Exercise.new
exercise.name = "foo"
exercise.description = "bar"
exercise.unit_id = 123
I am trying to do this and I keep getting
ActiveRecord::ConnectionNotEstablished - ActiveRecord::ConnectionNotEstablished:
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
/usr/lib/ruby/1.8/active_record/base.rb:1271:in `columns'
/usr/lib/ruby/1.8/active_record/base.rb:3014:in `attributes_from_column_definition_without_lock'
/usr/lib/ruby/1.8/active_record/locking/optimistic.rb:55:in `attributes_from_column_definition'
/usr/lib/ruby/1.8/active_record/base.rb:2434:in `initialize'
main.rb:14:in `new'
I just want to create and initialize the object then call a .save method. If I am going about this incorrectly, please let me know.
Short Answer: Yes, you need a connection!
You cannot (generally) use ActiveRecord unless there is a connection.
The set of attributes on the object is determined by the columns on the table. Which means you can't use the object unless you have the connection.
In any case what would happen after calling exercise.save if there is no connection?
So if you only need the validation, you can reuse ActiveSupport::Validation on a transient class.
This would imply that Rails cannot connect to your database. Ensure your database server is on (if applicable), and that your config/database.yml file is correct.
Try this at the top of your code
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "<your database username>",
:password => "<your database password>",
:database => "<your database name>"
)
The above code should establish an ActiveRecord database connection to your database. You can check out the manual for it here http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-establish_connection. Good luck!

Resources