ActiveRecord without Rails

A

Ari Brown

BEGIN { puts "hey all" }
hey all

Can anyone point me to a good tutorial for using ActiveRecord without
Rails?
Right now I'm just stealing what information I can from various sources.


Thanks,
aRi
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 
B

Ben Bleything

Can anyone point me to a good tutorial for using ActiveRecord without
Rails?
Right now I'm just stealing what information I can from various sources.

What do you need to know? You can (more or less) just install
activerecord, require it, and go to town. You still have to define your
"models" just as you would in Rails, but they don't need to be in
magically named files or anything fancy like that.

You do need to establish the connection by hand, but that's usually as
easy as:

ActiveRecord::Base.establish_connection({
:adapter => 'sqlite3',
:dbfile => '/home/ben/.sqlite/counts.sqlite'
})

of course, whatever would normally go in the database section of your
configuration goes into that little hash.

I use AR outside of Rails every day and haven't had much trouble. If
you're having specific problems, let us know... but for the most part,
there's nothing scary about doing it, just go for it :)

Ben
 
R

Reid Thompson

in case you're not limiting yourself to ActiveRecord...
see Og

http://www.nitroproject.org/docs/rdoc/index.html


rthompso@jhereg: ~$ cat simpleSqlite3.rb
require 'rubygems'
require 'sqlite3'
require 'og'

class SimpleTest
property :name, String
property :ts, String
# property :eek:id, Integer

# set_table :simpletest
end

$DBG=true
og_sqlite = {
:destroy_tables => false,
:store => :sqlite,
:user => 'rthompso',
:password => 'rthompso',
:name => 'test'
}

db = Og.setup(og_sqlite)

store = db.get_store

st = SimpleTest.new
st.name = 'Test entry'
st.ts = Time.now.to_s
st.save

rthompso@jhereg: ~$ ruby simpleSqlite3.rb
INFO: Og uses the Sqlite store.
DEBUG: Og manageable classes: [SimpleTest]
DEBUG: CREATE TABLE ogsimpletest (name text, ts text, oid integer PRIMARY KEY)
INFO: Created table 'ogsimpletest'.
DEBUG: SELECT * FROM ogsimpletest LIMIT 1
DEBUG: SELECT * FROM ogsimpletest LIMIT 1
DEBUG: INSERT INTO ogsimpletest (oid, name, ts) VALUES (NULL, 'Test entry', 'Tue Jul 31 15:28:10 -0400 2007')
DEBUG: SELECT last_insert_rowid()

rthompso@jhereg: ~$ sqlite3 ./test.db "select * from ogsimpletest"
Test entry|Tue Jul 31 15:28:10 -0400 2007|1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top