Ruby on Rails Tuesday, June 1, 2010

On 1 June 2010 15:51, Tom Ha <lists@ruby-forum.com> wrote:
> It's just that based on the API, I couldn't figure out if/where/how to
> place the "count" part in the find statement...

The problem is a little circular - as the count is nothing to do with
Rails, it's a function of the DB, and you need to know how the SQL
works to be able to use the finder correctly. The API tells you you
*can* use :having and :group; but also that these are just the clauses
in SQL - it's hardly up to Ruby/Rails APIs to then teach us how our DB
of choice implements these options to select records.

If you were to do it in Ruby, with disregard to the DB, you *could* do
something like:

books_with_over_five_authors = Book.all.select { |book|
book.authors.size > 5 }

It would probably (almost certainly) be *better* (in many ways) to do
it with parameters on the finder (but you need to know the SQL to
structure them correctly ;-)

PS In re-reading my earlier posts I sound a little stroppy/terse...
maybe I'm just in a "Marnen" mood today... apologies if it wasn't
apparent that I'm actually keen for you to understand and solve your
problem.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment