0

I am trying to build a friendship system sorta following this link: How to Implement a Friendship Model in Rails 3 for a Social Networking Application?. However the code seem to be very outdated, and I might of change it, what i am trying to do atm is simply to create a relationship, but this seem to not works.

So here my create

  #Send a friendship request
  def create
    Friendship.request(@customer, @friend)
    redirect_to friendships_path
  end

Which then technically would called the method request located in model which his already implemented in the previous post.

  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

and i also added these to the model

attr_accessible :status, :customer_id, :friend_id, :customer, :friend

However the friendship doesn't get created. Any reason why not? I call the relationship has follow

<%= link_to "Add friend", friendships_path(:friend_id => customer), :method => :post %>
Community
  • 1
  • 1
Jseb
  • 1,926
  • 4
  • 29
  • 51
  • Im trying to implement a friendship model myself and I am confused as to what I should be adding in attr_accessible. I would think status should be the only attribute in there. Isn't there a risk of malicious Users changing id's and thereby changing friendships between users? – pratski Mar 26 '13 at 14:49

1 Answers1

0

You need to separate the @customer and @friend. In your link, you are setting :friend_id to the customer, and you are never setting the @customer id.

Try this:

def create
  @customer = current_account
  @friend = Account.find(params[:friend_id])
  Friendship.request(@customer, @friend)
  redirect
end

In the link_to you need:

<%= link_to "Add Friend", friendships_path(:friend_id => friend),: method => :post %>
Solomon
  • 6,145
  • 3
  • 25
  • 34