ok - I guess I was wondering if there were a way to do it in Rails
non-procedurally, like using some of the activerecord facilities like named
scopes, etc.  I wonder if it's possible to somehow use associations in a
:conditions to somehow solve it?

On Mon, Jan 19, 2009 at 8:11 PM, MaD <[email protected]> wrote:

>
> > > Can I get an ActiveRecord.find to do this query?:   *Get all Articles
> > > which have only been published in less than X Magazines.*
>
> actually i think (and i'd not be very surprised if i was wrong with
> this one) you can't do that in a simple call. however what you can do
> is simulate it like this (in your Article model):
>
>  def self.all_with_less_publications_than(limit)
>    result_set = []
>    Articles.all.each do |article|
>      if article.magazines.count < limit
>        result_set << article
>      end
>    end
>    return result_set
>  end
>
> now you'll be able to call:
>
>  Article.all_with_less_publications_than(5)
>
> basically it does the same as your SQL statement. running through all
> articles and counting the related magazines. this is obviously not the
> shortest way to write such a method, but for the sake of readability
> it's imho best to write it like that.
>
> >
>


-- 
Greg
http://blog.gregnet.org/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to