On Saturday, April 28, 2012 1:13:39 AM UTC-7, John wrote:
>
> Hi there,
>
> I am new to Sequel and it looks really promising. Impressed so far.
>
> I would like to define my Sequel models (linked to a database table)
> *before* the connection is established, is that possible?
>
> class Contact < Sequel::Model(:ZABCDRECORD)
> end
>
> class AddressBookImporter
> def open_archive(archive)
> Sequel.sqlite(archive)
> end
> end
>
> Here I am trying to link the Contact model with the basic contact
> information table form the Address Book SQLite database. Sequel tells me:
>
> No database associated with Sequel::Model: have you called
> Sequel.connect or Sequel::Model.db= ?
>
> I can move the Sequel.sqlite command all the way to the top to make it
> work off course but the whole idea is to load it later as I don't know in
> advance what Address Book archive will be loaded (= the path to the SQLite
> database is given as a parameter).
>
> Thanks for the help!
>
The requirement to having an existing Database instance is by design. When
the model class is created, Sequel parses the database schema to set up
column methods and other things. You can work around this using anonymous
model classes and calling set_dataset/dataset= later:
Contact = Class.new(Sequel::Model)
DB = Sequel.sqlite(archive)
Contact.dataset = DB[:ZABCDRECORD]
But this usage is not recommended or officially supported. Why do you need
to load your models before connecting the database? This question comes up
occasionally, and almost everyone who thinks about it discovers that it
isn't actually a requirement. In your case, you could probably do:
class AddressBookImporter
def open_archive(archive)
Sequel.sqlite(archive)
# require model class file(s) here
end
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sequel-talk/-/9IrC6hfPxMsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.