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?
Woops I mean count(:limit=>1)
Aryk G. 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?
or should i just use count without the Limit 1
On Apr 15, 2007, at 10:56 PM, Aryk G. wrote:
or should i just use count without the Limit 1
Why not:
begin
find
rescue
# doesn’t exist
end
–
– Tom M., CTO
– Engine Y., Ruby on Rails Hosting
– Reliability, Ease of Use, Scalability
– (866) 518-YARD (9273)
On 4/15/07, Aryk G. [email protected] wrote:
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
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?
Hi Aryk,
Aryk G. 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
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?
Hey pat, I think I might just do that. Thanks for the response. =)
On 4/16/07, Aryk G. [email protected] 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