On Aug 30, 8:01 pm, Jeremy Evans <[email protected]> wrote:
> On Aug 30, 6:54 pm, james <[email protected]> wrote:
>
> > Hi,
>
> > Is it possible to combine separate datasets with Sequel?  This is an
> > example from Rails 3 scopes/Arel that I came across.
>
> > class User < ActiveRecord::Base
> >   scope :published, lambda {
> >     joins(:posts).group("users.id") & Post.published
> >   }
> > end
>
> > Or just:
> > User.joins(:posts).group("users.id") & Post.published
>
> I'm not sure what that does in AR.  If you show the SQL it produces, I
> could probably show you how to do it in Sequel.

class Post < ActiveRecord::Base
  scope :published, lambda {
    where("posts.published_at IS NOT NULL AND posts.published_at
<= ?", Time.zone.now)
  }
end

class User < ActiveRecord::Base
  scope :published, lambda {
    joins(:posts).group("users.id") & Post.published
  }
end

User.published.to_sql
  #=> SELECT users.* FROM "users"
  #   INNER JOIN "posts" ON "posts"."author_id" = "users"."id"
  #   WHERE (posts.published_at IS NOT NULL AND posts.published_at <=
'2010-02-27 02:55:45.063181')
  #   GROUP BY users.id

This is taken from:
http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
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