Newspiritcompany at InfogamiNewspiritcompany - Infoware Development ruby connect active recordHere is an example script for connecting to a database with active record and then saving a new dummy record. And it is quite obvious that ActiveRecord is doing all the work. You will need to install ruby and of course get Ruby on Rails or ActiveRecord and then create the database schema and launch the script.
#
# Berlin Brown
# Used to test active record connection
# Run with:
#
# ruby connect_db.rb
#
require 'active_record'
require 'logger';
# Use the following to reformat the logging message:
# class Logger; def format_message(severity, timestamp, msg, progname) "#{msg}\n" end; end
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "USER_NAME",
:password => "PASSWORD",
:database => "botverse_development"
)
@logger = Logger.new(STDOUT)
## Continue
## Active Record Class setup ---------------
class NumbersLink < ActiveRecord::Base
end
##
## Main Entry Point
##
def ruby_main
@logger.info "Connecting."
# Create a dummy record
link = NumbersLink.new()
link.main_url = "http://www.yahoo.com"
link.url_title = "The Title"
link.created_on = Time.now
if link.save
@logger.info("Record was successfully created.")
else
@logger.info("ERR: Could not create record")
end
@logger.info "Done."
end
ruby_main
##
## Once the record is saved, you will get the following message:
##
## Record was successfully created.
## End of File
Database Schema (MySQL) -- Create the simple user blog -- Users have user-links CREATE TABLE numbers_links ( id int(11) NOT NULL auto_increment, main_url varchar(255) NOT NULL, url_title varchar(128) NOT NULL, url_description varchar(255), source varchar(255), keywords varchar(255), created_on DATETIME DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id) ); last updated 2 years ago # |