Hello, i have the following models in my application: class Message < ActiveRecord::Base has_many :comments, :as => :commentable, :dependent => :destroy, :order => ''created_at ASC'' end class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end I need "commented" named scope, which do the follownig thing: Message.commented -> returns all messages where comments is > 0. How i can do this? -- Posted via http://www.ruby-forum.com/.
Joao Silva
2009-Jul-22 14:12 UTC
Re: How to build scope for count() & has many polymorphic?
anybody knows? -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-Jul-23 10:21 UTC
Re: How to build scope for count() & has many polymorphic?
The typical way around this is to turn on counter caching for :comments, and filter against that. --Matt Jones On Jul 22, 8:18 am, Joao Silva <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, i have the following models in my application: > > class Message < ActiveRecord::Base > has_many :comments, :as => :commentable, :dependent => :destroy, > :order => ''created_at ASC'' > end > > class Comment < ActiveRecord::Base > belongs_to :commentable, :polymorphic => true > end > > I need "commented" named scope, which do the follownig thing: > > Message.commented -> returns all messages where comments is > 0. > > How i can do this? > -- > Posted viahttp://www.ruby-forum.com/.