I have a check between friends that is happening on each listing. Right now I do @c.friends.find(:first, :conditions=> ["id=?",users]) This seems kind of overkill. For something like this, would you guys recommend: @c.friends.count(:first, options={}) Is that the least db intensive way of doing it? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz
2007-Apr-16 05:51 UTC
Re: best way to check if record exists? count with limit 1?
Woops I mean count(:limit=>1) Aryk Grosz wrote:> I have a check between friends that is happening on each listing. > > Right now I do > > @c.friends.find(:first, :conditions=> ["id=?",users]) > > This seems kind of overkill. > > > For something like this, would you guys recommend: > > @c.friends.count(:first, options={}) > > Is that the least db intensive way of doing it?-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz
2007-Apr-16 05:56 UTC
Re: best way to check if record exists? count with limit 1?
or should i just use count without the Limit 1 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Mornini
2007-Apr-16 07:58 UTC
Re: best way to check if record exists? count with limit 1?
On Apr 15, 2007, at 10:56 PM, Aryk Grosz wrote:> or should i just use count without the Limit 1Why not: begin find rescue # doesn''t exist end -- -- Tom Mornini, CTO -- Engine Yard, Ruby on Rails Hosting -- Reliability, Ease of Use, Scalability -- (866) 518-YARD (9273) --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pat Maddox
2007-Apr-16 08:24 UTC
Re: best way to check if record exists? count with limit 1?
On 4/15/07, Aryk Grosz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have a check between friends that is happening on each listing. > > Right now I do > > @c.friends.find(:first, :conditions=> ["id=?",users]) > > This seems kind of overkill. > > > For something like this, would you guys recommend: > > @c.friends.count(:first, options={}) > > Is that the least db intensive way of doing it?@c.friends.find_by_id params[:friend_id] that will return nil if the user doesn''t exist. If you don''t need to load the friend object into memory, sure, you could do a count instead of a find. There''s really nothing intensive about this, assuming you have indexes setup. Considering that one user should probably only be able to be friends with another user once, you can create a unique index, which will be very zippy. Also, be on the lookout for signs that you might want to promote a Friendship to its own abstraction. Pat --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz
2007-Apr-16 16:57 UTC
Re: best way to check if record exists? count with limit 1?
The thing is that I''m not looking up the record on its ID column, im looking it up on a combination of attributes. Nevertheless, If I use find first, it will return the object into memory. If I use count, than it will count all the occurences. Isn''t the best way to just test if it exists to do a count with a limit 1? Wouldn''t that be the least database intensive. Is there a functon right now to do this besides having to do Model.count(:limit=>1, :conditions=>"user_id=4 and book_id=7") > 0 ? OR Am I just plain being to picky and should just use find :first? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bill Walton
2007-Apr-16 17:26 UTC
Re: best way to check if record exists? count with limit 1?
Hi Aryk, Aryk Grosz wrote:> Isn''t the best way to just test if it exists to do a count with > a limit 1?The best way would probably be to use the exists? method. I''m not sure of the exact syntax, but for a start you might try... answer = Model.exists?(["user_id = ? and book_id = ?", 4, 7]) hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz
2007-Apr-16 22:22 UTC
Re: best way to check if record exists? count with limit 1?
Bill, If you look at the code for the "exists?" function, you''ll see that its really just another way of writing find :first. Which leads me to ask: Why not just use count with limit 1 instead of find with limit 1? I just don''t get it. It is no extra code to achieve the same results faster. What am I missing here? -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pat Maddox
2007-Apr-16 22:45 UTC
Re: best way to check if record exists? count with limit 1?
On 4/16/07, Aryk Grosz <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Bill, > > If you look at the code for the "exists?" function, you''ll see that its > really just another way of writing find :first.That''s the difference between interface and implementation. You can look at exists? and know what it does without caring how it does it.> Which leads me to ask: > > Why not just use count with limit 1 instead of find with limit 1? > > I just don''t get it. It is no extra code to achieve the same results > faster. What am I missing here?Nothing. You''re just looking way too much into this. Go ahead and use count if you really want to. Heck, you can even override AR::Base.exists? to use a count implementation instead of find, if that''s what you want. Pat --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Aryk Grosz
2007-Apr-16 22:59 UTC
Re: best way to check if record exists? count with limit 1?
Hey pat, I think I might just do that. Thanks for the response. =) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---