Active Record Could not find table - ruby

I'm learning Active Record and have written a simple example below:
#!/usr/bin/ruby
require 'active_record'
require 'sqlite3'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'test.sqlite3',
)
class Network < ActiveRecord::Base
end
network = Network.create(name: "Network1")
puts Network.all
but this results in an error message:
/var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:511:in `table_structure': Could not find table 'networks' (ActiveRecord::StatementInvalid)
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:385:in `columns'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/attributes.rb:93:in `columns'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/attributes.rb:98:in `columns_hash'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/inheritance.rb:54:in `new'
from /var/lib/gems/2.1.0/gems/activerecord-4.2.5/lib/active_record/persistence.rb:33:in `create'
from test.rb:14:in `<main>'
What am I doing wrong?

Your database doesn't have the networks table. Thus ActiveRecord will throw that error when trying to persist your data.
You should use create_table method in order to create your table.
create_table(:networks) do |t|
t.column :name, :string
end

Related

null validation failed when attribute supplied rom-rb

I'm trying to get to grips with the rom-rb persistence library, using sqlite3.
I ran the following migration, which includes a NOT NULL constraint:
ROM::SQL.migration do
change do
create_table :users do
primary_key :id
column :name, String, null: false
column :age, Integer
column :is_admin, TrueClass
end
end
end
Here's my simple app.rb:
require 'rom'
rom = ROM.container(:sql, 'sqlite://db/my-db-file.db') do |config|
class Users < ROM::Relation[:sql]
schema(infer: true)
end
config.relation(:users)
end
users = rom.relations[:users]
puts users.to_a.inspect # => []
create_user = users.command(:create)
create_user.call( name: 'Rob', age: 30, is_admin: true )
puts users.to_a.inspect # never reached
Trying to run this script produced the following output:
Roberts-MacBook-Pro:my-rom-demo Rob$ ruby app.rb
[]
/Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step': SQLite3::ConstraintException: NOT NULL constraint failed: users.name (ROM::SQL::NotNullConstraintError)
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `to_a'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `block in execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:189:in `block (2 levels) in _execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/logging.rb:38:in `log_connection_yield'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:189:in `block in _execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/connecting.rb:253:in `block in synchronize'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/connection_pool/threaded.rb:91:in `hold'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/database/connecting.rb:253:in `synchronize'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:180:in `_execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/adapters/sqlite.rb:146:in `execute_insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/dataset/actions.rb:1099:in `execute_insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/sequel-5.11.0/lib/sequel/dataset/actions.rb:399:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/relation/writing.rb:39:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `block in insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `map'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:46:in `insert'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/create.rb:31:in `execute'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-core-4.2.1/lib/rom/command.rb:280:in `call'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-sql-2.5.0/lib/rom/sql/commands/error_wrapper.rb:16:in `call'
from /Rob.rvm/gems/ruby-2.4.0#learn-rails/gems/rom-core-4.2.1/lib/rom/commands/composite.rb:17:in `call'
from app.rb:15:in `<main>'
Why does it think my name attribute is null when I'm providing it?
NOTE: I revised my answer after some testing and learning about ops gem versions
The reason you're getting a NULL CONSTRAINT error is because ROM does not
have a schema loaded for the the users table.
When you defined the container below
rom = ROM.container(:sql, 'sqlite://db/my-db-file.db') do |config|
class Users < ROM::Relation[:sql]
schema(infer: true)
end
config.relation(:users)
end
you defined two things, a relation class bound to a constant called Users and an auto generated relation with the same name but is actually registered inside the ROM container. Effectively the Users constant relation is being ignored. The reason this is important is because the auto generated relation isn't automatically inferring the schema from the database so when you go to write data out, the schema forces all of the unknown keys to be removed causing the error. All you're sending to the db is {}.
To fix the error just tell the relation to infer the schema - an example can be seen below.
require 'rom'
require 'rom/sql'
require 'sqlite3'
puts "ROM Version #{ROM::Core::VERSION}" # 4.2.1
puts "ROM Version #{ROM::SQL::VERSION}" # 2.5.0
puts "Sequel Version #{Sequel::VERSION}" # 5.11.0
puts "SQLite3 Gem Version #{SQLite3::VERSION}" # 1.3.13
opts = {
adapter: :sqlite,
database: 'c:/mydb.db'
}
rom = ROM.container(:sql, opts) do |c|
# Just another way to write the same users table
# c.gateways[:default].create_table(:users) do
# column :id, :integer, primary_key: true
# column :name, :string, null: false
# column :age, :integer
# column :is_admin, :bool
# end
c.gateways[:default].create_table :users do
primary_key :id
column :name, String, null: false
column :age, Integer
column :is_admin, TrueClass
end
c.relation(:users) do
schema(infer: true)
end
end
users = rom.relations[:users]
puts users.to_a.inspect # => []
create_user = users.command(:create)
create_user.call(name: 'Rob', age: 30, is_admin: true)
puts users.to_a.inspect # never reached
# Uncomment if you want to see the users schema
# puts users.dataset.db.schema(:users)
If you want to use standalone relation classes instead of the container config dsl then I suggest reading up on the Auto Registration system.
DATABASE CREATION ISSUE
There is a whole host of things that could be going on which could prevent a sqlite database from being created.
It could be a permissions issue
The directory structure might not exist
Sqlite might not be compiled to handle URI's (only matters if you are using file:// in your paths) [see sqlite docs]
My advice here is when working with sqlite and ROM, use the opts hash example from the script above and try and use a relative path from the current working directory. That seems to always work.

unable to specify value in ActiveRecord call (Ruby, Sinatra, not Rails)

I have a reference to a Model object called Admin which has a field called id. The object property is accessible in the puts line. I now need to pass in that ID into an ActiveRecord call to create another object (where it serves as foreign key) as follows but it throws an exception copied below (the admin.id does not get picked up in the Bill.create call):
admin = Admin.find_by(email:email)
puts "admin id #{admin.id}" # this gets printed with correct value
bill = Bill.create(admin_id: admin.id, body: body)
Exception:
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attribute_assignment.rb:59:in `rescue in _assign_attribute': unknown attribute 'admin_id' for Bill. (ActiveRecord::UnknownAttributeError)
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attribute_assignment.rb:35:in `each'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/core.rb:564:in `init_attributes'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/core.rb:281:in `initialize'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:61:in `new'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:61:in `new'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/persistence.rb:33:in `create'
from test.rb:34:in `<main>'
ActiveRecord migration for the Bills model:
def up
create_table :bills do |t|
t.integer :admin_id
t.text :body
end
add_foreign_key :bills, :admins
end
Thank you in advance for sharing insights on what I am doing wrong.
Per my comment, you should really make this into a belongs_to relation, here's the refactored migration (using latest Rails 4 syntax):
class CreateBills < ActiveRecord::Migration
def change
create_table :bills do |t|
t.references :admin
t.text :body
end
end
end
add these relations to the models:
class Admin < ActiveRecord::Base
has_many :bills
end
class Bill < ActiveRecord::Base
belongs_to :admin
end
and the code for creating a bill associated with an admin:
admin = Admin.find_by(email:email)
puts "admin id #{admin.id}" # this gets printed with correct value
bill = admin.bills.create(body: body)

Association for table less model

I've been trying to implement the association at FrontEnd but as currently the application in not having any database directly connected with the website, so as a result we can not use the ActiveRecord and only using the ActiveModel for supporting the validations and core features of a Model. Now as we need to use the nested attributes which we are going to send along with an object, the addresses which are associated with the User, so for this we need to first define the association on the corresponding model. But after defining the association it is throwing exception of undefined method "has_many" on User model. I'm currently searching the way to implement it in our website and implement the logic of nested attributes.
It would be great if you can suggest me anything related to this or if you have met with such issue in the past.
I've also tried the approach using the gem https://github.com/softace/activerecord-tableless but not working for me. Also I've added a tableless.rb
tableless.rb
class Tableless < ActiveRecord::Base
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null )
end
def self.columns()
#columns ||= [];
end
def self.columns_hash
h = {}
for c in self.columns
h[c.name] = c
end
return h
end
def self.column_defaults
Hash[self.columns.map{ |col|
[col.name, col.default]
}]
end
def self.descends_from_active_record?
return true
end
def persisted?
return false
end
# override the save method to prevent exceptions
end
But getting the following exception Exception:
Console Error:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:203:in `table_exists?'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:92:in `get_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:77:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:79:in `reset_primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:32:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:70:in `write_attribute'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:19:in `__temp__9646='
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:13:in `block in initialize'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `each'
from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `initialize'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
from (irb):19
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/cis/.rvm/gems/ruby-2.0.0-p0#website/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
It seems that you forgot to call the method
has_no_table
On your model, as per https://github.com/softace/activerecord-tableless#usage. In their example:
class ContactMessage < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
validates_presence_of :name, :email
end
Hope this helps. =)

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Trying to use multiple databases - getting ActiveRecord Connection Not Established

I'm trying to set up ActiveRecord so I can use multiple database connections in my application. I am not using Rails.
To do this, I set up an abstract class for each of my databases:
class BuoyDatabase < ActiveRecord::Base
self.abstract_class = true
establish_connection $database_config['buoy_database']
end
Then I inherit from the OneDatabase class:
class Buoy < BuoyDatabase
has_many :buoyDatas, :foreign_key => 'buoy_id'
end
class BuoyData < BuoyDatabase
belongs_to :buoy
end
I can successfully create and read instances of both Buoy and BuoyData, but I can't get any related records:
Buoy.find_by_id(...).buoyDatas # gives ActiveRecord::ConnectionNotEstablished
Details of the error:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:69:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:54:in `initial_count_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:12:in `block in initialize'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `yield'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `default'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `aliased_name_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:17:in `aliased_table_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:15:in `block in construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `each'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:53:in `add_constraints'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:33:in `scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:99:in `association_scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:88:in `scoped'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:367:in `find_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:324:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:51:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
The workaround is to establish a connection on ActiveRecord::Base itself. It doesn't matter to which database the connection is made. This connection is only needed so that ActiveRecord can get the meta information necessary to handle associations and whatnot. You need not use the connection at all, once established.
This issue is a duplicate of ActiveRecord 3.1.0 multiple databases and the workaround is first described here: https://stackoverflow.com/a/7406374/29729

Resources