Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sqlite with activerecord outside rails
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => ":memory:"
)
ActiveRecord::Schema.define do
create_table :albums do |table|
table.column :title, :string
table.column :performer, :string
end
create_table :tracks do |table|
table.column :album_id, :integer
table.column :track_number, :integer
table.column :title, :string
end
end
class Album < ActiveRecord::Base
has_many :tracks
end
class Track < ActiveRecord::Base
belongs_to :album
end
album = Album.create(:title => 'Black and Blue',
:performer => 'The Rolling Stones')
album.tracks.create(:track_number => 1, :title => 'Hot Stuff')
album.tracks.create(:track_number => 2, :title => 'Hand Of Fate')
album.tracks.create(:track_number => 3, :title => 'Cherry Oh Baby ')
album.tracks.create(:track_number => 4, :title => 'Memory Motel ')
album.tracks.create(:track_number => 5, :title => 'Hey Negrita')
album.tracks.create(:track_number => 6, :title => 'Fool To Cry')
album.tracks.create(:track_number => 7, :title => 'Crazy Mama')
album.tracks.create(:track_number => 8,
:title => 'Melody (Inspiration By Billy Preston)')
album = Album.create(:title => 'Sticky Fingers',
:performer => 'The Rolling Stones')
album.tracks.create(:track_number => 1, :title => 'Brown Sugar')
album.tracks.create(:track_number => 2, :title => 'Sway')
album.tracks.create(:track_number => 3, :title => 'Wild Horses')
album.tracks.create(:track_number => 4,
:title => 'Can\'t You Hear Me Knocking')
album.tracks.create(:track_number => 5, :title => 'You Gotta Move')
album.tracks.create(:track_number => 6, :title => 'Bitch')
album.tracks.create(:track_number => 7, :title => 'I Got The Blues')
album.tracks.create(:track_number => 8, :title => 'Sister Morphine')
album.tracks.create(:track_number => 9, :title => 'Dead Flowers')
album.tracks.create(:track_number => 10, :title => 'Moonlight Mile')
puts Album.find(1).tracks.length
puts Album.find(2).tracks.length
puts Album.find_by_title('Sticky Fingers').title
puts Track.find_by_title('Fool To Cry').album_id
@joeletizia

This comment has been minimized.

Copy link

commented Apr 17, 2013

This helped me out. Thanks.

@padwasabimasala

This comment has been minimized.

Copy link

commented Jul 11, 2013

Very helpful, thank you.

I'm using ActiveRecord 4.0.0 and had to change the following

ActiveRecord::Base.logger = Logger.new(STDERR)
#ActiveRecord::Base.colorize_logging = false

ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :database  => ":memory:" # not dbfile
)
@Integralist

This comment has been minimized.

Copy link

commented Oct 5, 2013

I get an error about "album already exists"? Any ideas @padwasabimasala @unnitallman

@Integralist

This comment has been minimized.

@ghost

This comment has been minimized.

Copy link

commented Nov 25, 2013

Very helpful, thanks! I'm using activerecord 4.0.1 and had to make the changes padwasabimasala pointed out.

@mmzoo

This comment has been minimized.

Copy link

commented Jul 10, 2014

Beautiful, thank you

@zoopyserg

This comment has been minimized.

Copy link

commented Jul 20, 2014

Thx bro

@fuzzy

This comment has been minimized.

Copy link

commented Jul 27, 2014

handy.

@ogaida

This comment has been minimized.

Copy link

commented Nov 8, 2018

very nice. You could change the last lines to:

Album.find_by_title('Sticky Fingers').tracks.each do |t|
  puts "#{t.title}"
end
puts Track.find_by_title('Fool To Cry').album.title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.