On Thursday, August 9, 2012 11:52:32 AM UTC-7, Sean Redmond wrote:
>
> I'm trying to define, as generically as possible, a bunch classes in gems 
> that will work together as a Sinatra application. Let's say the Sinatra app 
> is going to be in "myapp.rb" and my classes are in a "my_classes" gem. I 
> would expect in myapp.rb to be able to:
>
> 1) require my_classes
> 2) open a database connection
> 3) run off to the races...
>
> The snag that I'm running into is that when you write, in my_classes.rb, 
> say:
>
>    class MyClass < Sequel::Model[:mytable]
>
> Sequel expects to have a connection already open at "compile time."  This 
> means that you can't put "require 'sequel'" in my_classes.rb It seems you 
> have to do the following steps in the myapp.rb:
>
> 1) require sequel
> 2) open the database connection
> 3) require my_classes
>
> Not a big deal but it doesn't seem like the cleanest pattern. Is there 
> some other way that I'm missing?
>

This is expected behavior.  When you create the class, it needs to 
introspect the database to find the columns.  What's the problem with 
opening the database connection first?  The general way to do this is to 
just have the model code in a separate file(s), and don't require that file 
until after the database connection has been made.

If you really need to load the model classes before the database 
connection, you can create anonymous model classes, and don't call 
set_dataset on them until after the database connection has been made.  
However, you'll find that many things won't work if the model class does 
not have a dataset.

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/-/g2nSjb9pxBgJ.
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.

Reply via email to