Skip to content

Instantly share code, notes, and snippets.

@malclocke
Created October 30, 2018 22:43
Show Gist options
  • Save malclocke/45862765a6b19f08c87491995f427919 to your computer and use it in GitHub Desktop.
Save malclocke/45862765a6b19f08c87491995f427919 to your computer and use it in GitHub Desktop.
require 'sequel'
require 'logger'
DB = Sequel.sqlite(loggers: [Logger.new(STDOUT)])
DB.create_table :authors do
primary_key :id
String :name
end
DB.create_table :books do
primary_key :id
foreign_key :author_id
String :title
end
class Author < Sequel::Model
one_to_many :books
end
class Book < Sequel::Model
many_to_one :author
end
author = Author.create(name: 'Douglas Adams')
author.add_book(title: 'Hithikers guide to the galaxy')
puts author.associations[:books].inspect
# => nil
puts author.books.inspect
# I, [2018-10-31T11:39:43.254256 #18170] INFO -- : (0.000090s) SELECT * FROM `books` WHERE (`books`.`author_id` = 1)
# => [#<Book @values={:id=>1, :author_id=>1, :title=>"Hithikers guide to the galaxy"}>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment