On Wednesday, December 11, 2013 12:35:17 PM UTC-8, [email protected] 
wrote:
>
> I'm creating an application which uses multiple databases of objects. I'm 
> trying to switch it to using sequel, but am having problems doing this with 
> the modeling.
> Basically I want to be able to create any number of databases (in-memory 
> sqlite), and have a model which is specific to each database instance. It 
> kinda works, but I'm having issues when using model relations.
>
> I think posting some code is the best way of explaining what I'm doing:
>
> #!/usr/bin/ruby
> require 'sequel'
>
> # create the database
> def newdb
> db = Sequel.sqlite
>
> db.create_table(:albums) do
> String :name, :primary_key => true
> String :artist
> end
> db.create_table(:tracks) do
> String :name, :primary_key => true
> String :album
> foreign_key [:album], :albums, :on_delete => :cascade
> end
>
> db
> end
>
> # create the model
> db0 = newdb
> class Album < Sequel::Model(db0[:albums])
> unrestrict_primary_key
> end
> class Track < Sequel::Model(db0[:tracks])
> def_column_alias :album_name, :album
> many_to_one :album, :key => :album_name, :key_column => :album
> unrestrict_primary_key
> end
>
> # define a jukebox
> class Jukebox
> attr_reader :albums
> attr_reader :tracks
> def initialize
> @db = newdb
>
> @albums = Class.new(Album)
> @albums.db = @db
>
> @tracks = Class.new(Track)
> @tracks.db = @db
> end
> end
>
> jb0_album1 = Album.create(:name => 'jb0-album1')
> jb0_track1 = Track.create(:name => 'jb0-track1', :album => jb0_album1)
> puts Album['jb0-album1'].inspect
> puts Track['jb0-track1'].inspect
> puts Track['jb0-track1'].album.inspect
>
> puts '----'
>
> jb1 = Jukebox.new
> jb1_album1 = jb1.albums.create(:name => 'jb1-album1')
> jb1_track1 = jb1.tracks.create(:name => 'jb1-track1', :album => jb1_album1)
> puts jb1.albums['jb1-album1'].inspect
> puts jb1.tracks['jb1-track1'].inspect
> puts jb1.tracks['jb1-track1'].album.inspect
> ----
>
> Output:
> #<Album @values={:name=>"jb0-album1", :artist=>nil}>
> #<Track @values={:name=>"jb0-track1", :album=>"jb0-album1"}>
> #<Album @values={:name=>"jb0-album1", :artist=>nil}>
> ----
> #< @values={:name=>"jb1-album1", :artist=>nil}>
> #< @values={:name=>"jb1-track1", :album=>"jb1-album1"}>
> nil
> ----
>
> As you can see, they don't behave the same. With the Jukebox instance, the 
> album exists, but when trying to reference it via the Track#album method, 
> it gives back nil.
>
> Is there any way to accomplish what I'm trying to do?
>

Try moving your association into the subclass, after setting the database 
correctly:

 @tracks = Class.new(Track)
 @tracks.db = @db
 @tracks.many_to_one :album, :key => :album_name, :key_column => :album, 
:class=>@albums

The problem with your existing code is that @tracks uses the albums 
association from the superclass (Track), which is tied to the Album model, 
not @albums, and Album is tied to @db.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to