That did the trick. Thanks!
I copied your code into my Comment module and added:
def self.included(model)
register_commenting_model(model)
end
What about adding support for :limit. the only thing i can think of is
doing the following:
def self.comments_for_user(user)
commenting_models.inject([]) do |results, model|
results << model.all(:user => user, :order =>
[ :created_at.desc ], :limit => 10)
end.flatten.sort do |x,y|
x.created_at <=> y.created_at
end.first(10)
end
But that's kind of ugly since it would fetch rows that would then be
discarded.
cheers
-bryan
On Jan 29, 3:05 pm, Martin Gamsjaeger <[email protected]> wrote:
> Bryan,
>
> On Thu, Jan 29, 2009 at 19:40, Bryan <[email protected]> wrote:
>
> > Thanks for the response Martin.
>
> > I'm assuming I would then have to query each table seperately and
> > combine the results of each query into one array.
>
> Yep, I think that's the only way?
>
> > How would I go about combining several collections into one while
> > taking into account ordering? (i.e if i want to order the results by
> > created_at)
>
> This is thrown together very quickly and not tested at all, but maybe
> something
> along these lines does the trick. (I'm not really familiar with
> Array#sort though,
> but it should at least head into the right direction?)
>
> module Comment
> def self.register_commenting_model(model)
> (@commenting_models ||= []) << model
> end
> def self.commenting_models
> �...@commenting_models
> end
> def self.comments_for_user(user)
> commenting_models.inject([]) do |results, model|
> results << model.all(:user => user, :order => [ :created_at.desc ])
> end.flatten.sort do |x,y|
> x.created_at <=> y.created_at
> end
> end
> end
>
> # use it like so
> Comment.comments_for_user(User.get(1))
>
> HTH
> Martin
>
> > -bryan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" 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/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---