On 1 June 2010 14:03, Colin Law <[email protected]> wrote: > Michael > To some extent I agree with Tom here, ideally it should be possible to > get straight to the answer without going through the sql. The > condition is that the number of authors for the book should be greater > than 5 and > that published should be true, so the requirement is fully defined, > the question is how to tell active record that that is what is > required. To some extent the framework has failed (or one is just > trying to do something too complex for it) if one has to work out the > sql first then work out how to tell ActiveRecord to generate that sql.
Colin, I doubt that you or I would *need* to do the SQL first for our own models; but I often do so anyway before writing a complex finder as a safety-net to ensure Rails returns what I want. But if the OP makes up an example that doesn't match his models, then we can't post solutions. If he has no idea what the SQL needs to be to return data from his database, then maybe there's some more non-Rails learning to cover. It's similar to the argument that anti-IDE people use; when they say the IDE stops you understanding what's going on underneath. Well, if one doesn't understand the SQL generated by a find, then that's not a great situation. For instance, in an app I've worked on, there's a Person model that can have many Participations (not too dissimilar to Authors and Books). If I want to return everyone with a surname beginning with "B" who has more than one participation, I can use the following: Person.all(:select => "people.*, count(people.id)", :joins => :participations, :group => "people.id", :conditions => ["people.lastname LIKE ?", "b%"], :having => "count(people.id) > 1") ... and frankly, I can leave the :select out if I don't care about the amount of Participations, to let AR do its thing But unless the OP can be *sure* the results are correct for his implementation (by checking it against a SQL DB query) then it strikes me as a little bit of a worry. -- 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.

