Forum: Ruby on Rails Checking to see if a listing exists

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Dave A. (Guest)
on 2007-02-01 04:47

I'm writing a web app where people can add each other as friends, and
I'm having trouble with the add/remove friend link. I can't figure out
the SQL query that asks the database if there's a listing in the
friends_users table that matches the friend_id and user_id. Here's what
I have now:

    friend_query = "select u.*
      from users u, friends_users f
      where = f.friend_id and f.user_id = ? and f.friend_id = ?"

    @a_friend = User.find_by_sql([friend_query,,

    @a_friend.each do |result|
      @friendship =


<% if @friendship != nil %>
  ...remove this friend...
<% else %>
  ...add this friend...
<% end %>

This SQL query doesn't do exactly want I want (I'm not even sure exactly
what it returns). Is there a way to simply find out if a record in a
database exists based on two parameters (the friend_id and the user_id)?
alex (Guest)
on 2007-02-01 12:36
(Received via mailing list)
Try posting your question on

askegg (Guest)
on 2007-02-02 06:40
(Received via mailing list)
On Feb 1, 1:47 pm, "Dave A." <removed_email_address@domain.invalid> wrote:
>       from users u, friends_users f
> --
> Posted via

find_or_create_by is your friend (Dynamic attribute-based finders at
This topic is locked and can not be replied to.