On Feb 1, 1:47 pm, "Dave A."
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hi,
>
> 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:
>
> Controller:
>     friend_query = "select u.*
>       from users u, friends_users f
>       where u.id = f.friend_id and f.user_id = ? and f.friend_id = ?"
>
>     @a_friend = User.find_by_sql([friend_query, current_user.id,
> params[:id]])
>
>     @a_friend.each do |result|
>       @friendship = result.id
>     end
>
> View:
>
> <% 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)?
>
> --
> Posted viahttp://www.ruby-forum.com/.
find_or_create_by is your friend (Dynamic attribute-based finders at
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000989)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---