Search code examples
rubypostgresql-9.2sequel

Ruby: Outputting Sequel schema with associations


Similar to my other question, but in this case, I want to get the schema of Authors with its association (Books). Here's what my code looks like:

require 'sequel'
require 'json'

db = Sequel.connect('postgres://localhost/testing');

class Sequel::Model
  self.plugin :json_serializer
end

class Author < Sequel::Model(:author)
  one_to_many :books
end

class Book < Sequel::Model(:book)
end

But when I do Author.db_schema, I'm only given the Author class's information... not the associations that it contains.

{
  :id=>{
    :oid=>23,
    :db_type=>"integer",
    :default=>"nextval('author_id_seq'::regclass)",
    :allow_null=>false,
    :primary_key=>true,
    :type=>:integer,
    :ruby_default=>nil
  },
  :name=>{
    :oid=>1043,
    :db_type=>"character varying",
    :default=>nil,
    :allow_null=>true,
    :primary_key=>false,
    :type=>:string,
    :ruby_default=>nil
  }
}

Is it possible to show that there's at least some sort of association with Book, and if possible, maybe include that information into the Author schema? Something along the lines of this.


Solution

  • You probably want to call Author.all_association_reflections to get metadata on associations (db_schema just gives column schema metadata). See http://sequel.jeremyevans.net/rdoc/files/doc/reflection_rdoc.html for details on Sequel's reflection methods.