rahrahruby
rahrahruby

Reputation: 693

Using ActiveRecord

I am trying to create a ruby script that writes data to my database using an activerecord model that I already have setup. In my app/models directory I have a model that looks like this:

class Financials < ActiveRecord::Base
  attr_accessible :symbol, :cur_price

end

and in my ruby script I have the following attempt to make a connection to the database.

require 'active_record'
 require 'mysql2'



 ActiveRecord::Base.Financials(
      adapter:  'mysql2',
      host:     'localhost',
      database: 'financials',
      username: 'dbuser',
 )

When I run the script I get the following error:

.rvm/gems/ruby-2.0.0-p0/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined method `Financials' for ActiveRecord::Base:Class (NoMethodError)

not quite sure what I am doing wrong here.

Thanks in advance for your help

Upvotes: 0

Views: 418

Answers (2)

nicosantangelo
nicosantangelo

Reputation: 13736

Have you tried using establish_connection?

require 'active_record'
require 'mysql2'

ActiveRecord::Base.establish_connection(
     adapter:  'mysql2',
     host:     'localhost',
     database: 'financials',
     username: 'dbuser',
 )

# Do your work
class Financials < ActiveRecord::Base
    attr_accessible :symbol, :cur_price    
end

# (...)

Upvotes: 2

AnkitG
AnkitG

Reputation: 6568

Have a look at this script lying under rails active record example. At the start of this script is doing inserts. https://github.com/rails/rails/blob/master/activerecord/examples/performance.rb

From line 10 to 91 is something that you can do.

Upvotes: 1

Related Questions