On 22 Sep 2008, at 19:43, Ben Johnson wrote:
> > I know this is probably a noob question, but I needed a little > clarification. I did the following in the console of my app: > >>> User.find(:all, :conditions => "orders.id > 1", :joins >>> => :orders).size > => 1254 > >>> User.find(:all, :conditions => "orders.id > 1", :include >>> => :orders).size > => 934 > > A user has many orders. Why am I getting different results? When using > fields in a has_many relationship should I ALWAYS use the :include > option. I did the reverse too: > >>> Order.find(:all, :conditions => "users.id < 100", :joins >>> => :user).size > => 172 > >>> Order.find(:all, :conditions => "users.id < 100", :include >>> => :user).size > => 172 > > I'm getting the same result. What is the general rule of thumb on when > to use joins or includes? > See http://www.spacevatican.org/2008/6/22/the-difference-between-include-and-joins . The difference between your two cases is that a user can have many orders: for each user you'll get a result for each object, whereas an order can only ever have one user. Fred > Thanks for your help! > -- > Posted via http://www.ruby-forum.com/. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

