How do require a gem from a model or anywhere other than just environment.rb

Okay, how do you require a gem from within Rails within a model as
opposed to environment.rb.

Disclaimer : Ruby/Rails Nuby
Install:

[C:] C:\Develop\ruby\bin

gem install creditcard --include-dependencies

Successfully installed creditcard-1.0
Installing ri documentation for creditcard-1.0...
Installing RDoc documentation for creditcard-1.0...

Restarted server...

Source :

class ContractPayment < ActiveRecord::Base

  set_table_name "contract_payment"
  set_primary_key "contract_payment_id"
  set_sequence_name "contract_payment_id_seq"

  belongs_to :contract, :foreign_key => "contract_id"

  validates_presence_of :contract_id, :credit_card_type_code,
:credit_card_type_prefix, :credit_card_expiration_date,
:credit_card_number, :credit_card_validation_code
  validates_numericality_of :credit_card_number
  validates_length_of :credit_card_number, :within => 13..16

  require "creditcard"
...

Restarted server to pick model changes...

Result :

" TypeError in Offer renewalController#take_payment

CreditCard is not a module"

Okay, so what silly mistake am I making.

Thanks,

try:

-----contract_payment.rb-----

require_gem 'creditcard'
class ContractPayment < ActiveRecord::Base
  ...
end

Chris

require_gem is deprecated and does not do what you think it does. You shoudl just use plain old require. Since rubygems will already be loaded it will just work when you require the gem directly.

Cheers-
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)

yeah, until reading the post about rubygems beta 0.9.0.9, I never knew
that require_gem was deprecated. thanks for the heads up.

Chris

I'm back to try this again...

If I put this in environment.rb, it works... I don't like that though
because it loads it across the entire application regardless of
usage. If I put this like

require 'creditcard'

class ContractPayment < ActiveRecord::Base

....

end

I get

TypeError in Offer renewalController#take_payment

CreditCard is not a module

RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace

C:/Develop/ruby/lib/ruby/gems/1.8/gems/creditcard-1.0/lib/
creditcard.rb:28
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
#{RAILS_ROOT}/app/models/contract_payment.rb:1
#{RAILS_ROOT}/app/controllers/offer_renewal_controller.rb:73:in
`take_payment'
-e:4:in `load'
-e:4

C:/Develop/ruby/lib/ruby/gems/1.8/gems/creditcard-1.0/lib/
creditcard.rb:28
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:204:in
`load_without_new_constant_marking'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:204:in `load_file'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:203:in `load_file'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:95:in `require_or_load'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:249:in `load_missing_constant'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:453:in `const_missing'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:465:in `const_missing'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:261:in `load_missing_constant'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:469:in `const_missing'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:1095:in `send'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:1095:in `perform_action_without_filters'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:632:in `call_filter'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:619:in `perform_action_without_benchmark'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
C:/Develop/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/rescue.rb:83:in `perform_action'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:430:in `send'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:430:in `process_without_filters'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:624:in
`process_without_session_management_support'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/session_management.rb:114:in `process'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:330:in `process'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/dispatcher.rb:
41:in `dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:113:in `handle_dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:79:in `service'
C:/Develop/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/Develop/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:63:in `dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/
servers/webrick.rb:59
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/
server.rb:39
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
script/server:3

C:/Develop/ruby/lib/ruby/gems/1.8/gems/creditcard-1.0/lib/
creditcard.rb:28
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
#{RAILS_ROOT}/app/models/contract_payment.rb:1
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:204:in
`load_without_new_constant_marking'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:204:in `load_file'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:203:in `load_file'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:95:in `require_or_load'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:249:in `load_missing_constant'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:453:in `const_missing'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:465:in `const_missing'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:261:in `load_missing_constant'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:469:in `const_missing'
#{RAILS_ROOT}/app/controllers/offer_renewal_controller.rb:73:in
`take_payment'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:1095:in `send'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:1095:in `perform_action_without_filters'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:632:in `call_filter'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:619:in `perform_action_without_benchmark'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
C:/Develop/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/benchmarking.rb:66:in
`perform_action_without_rescue'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/rescue.rb:83:in `perform_action'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:430:in `send'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:430:in `process_without_filters'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/filters.rb:624:in
`process_without_session_management_support'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/session_management.rb:114:in `process'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/
action_controller/base.rb:330:in `process'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/dispatcher.rb:
41:in `dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:113:in `handle_dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:79:in `service'
C:/Develop/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/Develop/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/Develop/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/
webrick_server.rb:63:in `dispatch'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/
servers/webrick.rb:59
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/
server.rb:39
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `gem_original_require'
C:/Develop/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:343:in `new_constants_in'
C:/Develop/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/
active_support/dependencies.rb:496:in `require'
script/server:3
-e:4:in `load'
-e:4

This error occurred while loading the following files:
   creditcard

Am I just being dense?

Jason

TypeError in Offer renewalController#take_payment
CreditCard is not a module

Can you post the line of code where this error occurs?

Steve

Ezra’s not quite right. Yes require_gem is deprecated, but there is a replacement:

gem ‘gem_name’, ‘optional version specification’

aka

gem ‘credit_card’, ‘>0.5.0’

require ‘credit_card’

Jason