On Thursday, April 26, 2012 10:24:25 AM UTC-7, Rodrigo Rosenfeld Rosas
wrote:
>
> Sometimes we want to do something like:
>
> Page.eager_graph(:user).select(:pages__name, :users__name).first
>
> But this relies on the fact that we know the table names for our Sequel
> Models Page and User. What if they're set up this way?
>
Do you honestly have an app where you don't know the table names being used?
> class Page < Sequel::Model(:page)
> end
> class User < Sequel::Model(:jsec_user)
> end
>
Model.table_name gives the table name, if you don't actually know it in
advance.
> I guess we could define it like:
>
> class Page < Sequel::Model(:page__p)
> end
>
Model.table_name even handles this case, returning just the alias.
> But what if you need to do some query where there is a self reference,
> like if a page could have a parent page?
>
You join with an alias:
Page.join(:page___parent, :id=>:parent_id).
join(:page___grand_parent, :id=>:parent_id).
where(:parent__name=>..., :grand_parent__name=>...)
> So, I was thinking that we could handle aliasing in Sequel models like
> this:
>
> Page.as(:p).eager_graph(:user, as: :u).select(:p__name, :u__name).first
>
eager_graph automatically sets up aliases for you, using a deterministic
method that handles self references, so you should not need to do this
manually.
> Well, this is just an idea. Since I'm starting to use Sequel more often
> maybe there is already another way to handle this without being too verbose
> but I couldn't find it in the docs.
>
> By the way, I don't like the idea of adding extensions to core classes, so
> I stopped using MongoMapper and I'm requiring Sequel without the core
> extensions.
>
Well, since 3.34, you shouldn't need to use the core extensions, every core
extension has a similar method defined on the Sequel module.
> I can't seem to find much documentation on how to do certain things
> without those query extensions. For example, see the equivalent codes:
>
> where('email is not null')
> where{~ {email: null} }
>
> How to do the above without enabling the core extensions?
>
>
The simplest way to do that is:
exclude(:email=>nil)
That because a shortcut exists for this case (inverting a filter). A
general solution would be:
where(Sequel.~(:email=>nil))
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/-/POnPEUsIOTAJ.
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.