Jason Fb wrote in post #1072573:
You seem to have defined @customer twice, once in the setup_friends
method which is run first as a before_filter hook, and then again in the
index method. the one in the index method returns an array, since you
asked for @customer.all
Sorry for the late reply, after notice I did change many thing. ( redid
from scracth)
So here an updated code
Friendship Model ( and rename table to friendship )
class Friendship < ActiveRecord::Base
belongs_to :customer
belongs_to :friend, :class_name => 'Customer', :foreign_key =>
'friend_id'
validates_presence_of :customer_id, :friend_id
attr_accessible :approved, :customer_id, :friend_id
#Method
# Return true if the customers are (possibly pending) friends.
def self.exists?(customer, friend)
not find_by_customer_id_and_friend_id(customer, friend).nil?
end
# Record a pending friend request.
def self.request(customer, friend)
unless customer == friend or Friendship.exists?(customer, friend)
transaction do
create(:customer => customer, :friend => friend, :status => 'pending')
create(:customer => friend, :friend => customer, :status =>
'requested')
end
end
end
# Accept a friend request.
def self.accept(customer, friend)
transaction do
accepted_at = Time.now
accept_one_side(customer, friend, accepted_at)
accept_one_side(friend, customer, accepted_at)
end
end
# Delete a friendship or cancel a pending request.
def self.breakup(customer, friend)
transaction do
destroy(find_by_customer_id_and_friend_id(customer, friend))
destroy(find_by_customer_id_and_friend_id(friend, customer))
end
end
private
# Update the db with one side of an accepted friendship request.
def self.accept_one_side(customer, friend, accepted_at)
request = find_by_customer_id_and_friend_id(customer, friend)
request.status = 'accepted'
request.accepted_at = accepted_at
request.save!
end
end
Customer Model
class Customer < ActiveRecord::Base
...
#RELATIONSHIP
has_many :friendships
has_many :friends, :through => :friendships,
:conditions => "status = 'accepted'"
has_many :requested_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'requested'"
has_many :pending_friends,
:through => :friendships,
:source => :friend,
:conditions => "status = 'pending'"
end
Controller Friendship
class FriendshipsController < ApplicationController
before_filter :setup_friends
def accept
if @customer.requested_friends.include?(@friend)
Friendship.accept(@customer, @friend)
flash[:notice] = "Friendship Accepted"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
def decline
if @customer.requested_friends.include?(@friend)
Friendship.breakup(@customer, @friend)
flash[:notice] = "Friendship Declined"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
def cancel
if @customer.pending_friends.include?(@friend)
Friendship.breakup(@customer, @friend)
flash[:notice] = "Friendship Canceled"
elserequested
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
def delete
if @customer.friends.include?(@friend)
Friendship.breakup(@customer, @friend)
flash[:notice] = "Friendship Deleted"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
def index
@customers = Customer.all
end
#Send a friendship request
def create
Friendships.request(@customer, @friend)
end
private
def setup_friends
@customer = current_customer
@friend = Customer.find_by_id(params[:id])
end
end
Now here the issue i seem to not really understand but is to manager the
relationship and here my code atm.
This is in index.html.erb from friendship and list everything,
<div><h1>Friends</h1></div>
<% @customer.friends.each do |friend| %>
<%= link_to friend.incomplete_name, '#' %><br />
<% end %>
<div><h1>Request</h1></div>
<% @customer.requested_friends.each do |rf| %>
<%= link_to rf.incomplete_name, '#' %><br />
<% end %>
<div><h1>Pendings</h1></div>
<% @customer.pending_friends.each do |pf| %>
<%= link_to pf.incomplete_name, '#' %><br />
<% end %>
<div><h1>Users</h1></div>
<% @customers.each do |customer| %>
<% if current_customer != customer %>
<div><%= friendship_status(current_customer, customer) %></div>
<% unless Friendship.exists?(current_customer, customer) %>
<%= link_to "Request friendship with #{customer.first_name}",
{ :controller => "friendships", :action => "create",
:id => customer.id },
:confirm =>
"Send friend request to #{customer.incomplete_name}?" %>
<% end %>
<% end %>
<% end %>
In railspace they use an email link to accept, but i don't want to send
an email and not sure how to refresh page with the proper information.
Thanks in advance