Andrew Kim
Andrew Kim

Reputation: 3335

Getting an error when trying to require active_record

In my Gemfile I have:

source 'https://rubygems.org'

gem 'pry'
gem 'pg'
gem 'activerecord'

In my app.rb file I have:

require 'active_record'
require 'pg'
require 'pry'



ActiveRecord::Base.establish_connection(
    database: 'hospital_db',
    adapter: 'postgresql'
)

class Doctor < ActiveRecord::Base
  has_many :appointments, :skills
  has_many :patients, through: :appointments
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :doctors, through: :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :patient
  belongs_to :doctor
end

class Skill < ActiveRecord::Base
  belongs_to :doctor
end

binding.pry

As soon as I type ruby app.rb in my terminal I get the following errors:

/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::BASE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of BASE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_ALL
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_ALL was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_NaN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_NaN was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_INFINITY
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_INFINITY was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_UNDERFLOW
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_UNDERFLOW was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_OVERFLOW
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_OVERFLOW was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::EXCEPTION_ZERODIVIDE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of EXCEPTION_ZERODIVIDE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_MODE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_MODE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_UP
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_UP was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_DOWN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_DOWN was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_HALF_UP
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_HALF_UP was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_HALF_DOWN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_HALF_DOWN was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_CEILING
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_CEILING was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_FLOOR
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_FLOOR was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::ROUND_HALF_EVEN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of ROUND_HALF_EVEN was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_NaN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_NaN was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_POSITIVE_ZERO
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_POSITIVE_ZERO was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_NEGATIVE_ZERO
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_NEGATIVE_ZERO was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_POSITIVE_FINITE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_POSITIVE_FINITE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_NEGATIVE_FINITE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_NEGATIVE_FINITE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_POSITIVE_INFINITE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_POSITIVE_INFINITE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::SIGN_NEGATIVE_INFINITE
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of SIGN_NEGATIVE_INFINITE was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::INFINITY
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of INFINITY was here
/Users/andrewkim/.rvm/gems/ruby-2.1.5@global/extensions/x86_64-darwin-14/2.1.0-static/bigdecimal-1.2.5/bigdecimal.bundle: warning: already initialized constant BigDecimal::NAN
/Users/andrewkim/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:121: warning: previous definition of NAN was here
WARN: Unresolved specs during Gem::Specification.reset:
      json (>= 1.7.7, ~> 1.7)
      minitest (~> 5.1)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
/Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:64:in `initialize': undefined method `arity' for :skills:Symbol (NoMethodError)
    from /Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations/builder/collection_association.rb:18:in `initialize'
    from /Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:47:in `new'
    from /Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:47:in `create_builder'
    from /Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:35:in `build'
    from /Users/andrewkim/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/associations.rb:1259:in `has_many'
    from app.rb:13:in `<class:Doctor>'
    from app.rb:12:in `<main>'

I imagine it's something simple, but I've tried bundle install again, and I can't figure out why its trying to override BigDecimal Constant or how I can fix it. I tried requiring activerecord and active-record but they give an easy cannot load such file error. So require active_record has got to be the right one. Any thoughts or suggestions?

Upvotes: 0

Views: 716

Answers (2)

Beartech
Beartech

Reputation: 6411

This looks wrong to me and I have tried to find a reference to it in the API:

class Doctor < ActiveRecord::Base
  has_many :appointments, :skills

The correct syntax is:

 has_many :appointments
 has_many: skills

Upvotes: 1

Frederick Cheung
Frederick Cheung

Reputation: 84114

I'm not sure why bigdecimal is being loaded multiple times. However your Gemfile won't actually be used unless you put

require "bundler/setup"

At the top of the script.

The actual error however is because of the line

has_many :appointments, :skills

You can't list multiple associations like that - it needs to be two separate calls to has_many:

has_many :appointments
has_many :skills

Upvotes: 2

Related Questions