On 1 June 2010 12:15, Tom Ha <[email protected]> wrote: > Hi there, > > What's the correct end part of "conditions" if: > > - you assume that "Book :has_many Authors" > - I'd like to have the find statement in the below format > > @books = Book.find(:all, > :joins => [:authors => [...]], > :include => :whatever_stuff, > :conditions => "books.published = TRUE AND > number-of-authors-must-be-greater-than-5") >
The raw SQL will be along the lines of: SELECT books.*, count(books.id) AS amount_of_authors FROM books JOIN authors ON books.id = authors.book_id GROUP BY books.id HAVING count( books.id ) > 5 Get that working in your SQL query editor against the real DB to be sure the right syntax gives the right result for you, and then you should be able to turn that into an AR find with a bit of reference to the API. As an aside, I wonder about your model... when I've looked at things link Authors/Articles before, I've tended to have a join table like "Authorships", so that I can keep a unique instance of the information about the author as a person, and multiple records for their participation in books/articles/whatever. You still have a many:many relationship between books and authors, but you just go "through" authorships. -- 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.

